Data Structures MIDTERM REVIEW

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

The primary operations of data structures

1) Accessing or retrieving elements (e.g., accessing the first or last element of a list) 2) Searching for elements (e.g., finding the index of an element in an array) 3) Inserting elements (e.g., adding a new element to the end of a list) 4) Deleting elements (e.g., removing an element from the middle of an array) 5) Sorting elements (e.g., arranging elements in ascending or descending order) 6) Merging data structures (e.g., combining two sorted lists into one sorted list) 7) Traversing elements (e.g., visiting each element of a tree in a specific order)

What a circular array is.

A circular array is a data structure that uses a fixed-size array as if it were connected end-to-end in a circle. This means that when the end of the array is reached, the next element is the first element in the array.

What a stable sort vs. unstable sort is

A stable sort is a sorting algorithm that preserves the relative order of equal elements in a list. In other words, if two elements have the same value, their original order in the list is maintained after the sort. An unstable sort, on the other hand, is a sorting algorithm that does not necessarily preserve the relative order of equal elements in a list. In other words, if two elements have the same value, their original order in the list may not be maintained after the sort.

Interfaces

An interface is a collection of abstract methods that define a set of behaviors or capabilities that a class can implement. Example: //Define an interface for shapes interface Shape { function getArea(); function getPerimeter();

Abstract Classes

An interface is a point of interaction or communication between two systems, devices, or components, allowing them to exchange information and perform operations in a coordinated way. Example:

Worst, Average, Best case scenarios

Best case scenario is the set of all inputs that minimizes the algorithms runtime Worst case scenario is the set of all inputs that maximizes the algorithms runtime Average case scenarios is the set of all inputs that requires statistical analysis of random inputs. Much harder to reason about average case

Big O

Big O notation is a way to describe the upper bound of the time complexity of an algorithm or the worst-case scenario of its runtime. Example- f(n) <= C * g(n) for all n >= N

The drawbacks to BigO Analysis

Big O notation only considers the worst-case scenario and may not reflect the average or best-case performance of an algorithm. Big O notation assumes that all operations take the same amount of time, which is not always the case in practice.

Big Omega

Big Omega notation is a way to describe the lower bound of the time complexity of an algorithm or the best-case scenario of its runtime. Example- f(n) >= c * g(n) for all n >= N

Dynamic Programming

Break down a problem into smaller and smaller subproblems. At their lowest levels, the subproblems are solved and their answers stored in memory. These saved answers are used again with other larger (sub)problems which may call for a recomputation of the same information for their own answer. Reusing the stored answers allows for optimization by combining the answers of previously solved subproblems.

Contiguous memory

Contiguous memory refers to a block of memory in which each element is stored next to one another in a continuous sequence of addresses.

Theta

Example- f(n) = (g(n)) and f(n) = (g(n)) then f(n) = 0 (g(n))

First in, First out (FIFO)

First in, First out (FIFO) is a principle used in computer science and other fields to describe a system or structure where the first item added to the system or structure is the first item to be removed. (A QUEUE)

The algorithm for a binary search.

Given a sorted list of n elements and a target value to search for, set the lower bound (l) to the first index of the list, and set the upper bound (r) to the last index of the list.

Encapsulation

Hides the fine detail of the inner workings of the class- The implementation is hidden- Often called "information hiding" Example- person = new Person("Alice"); print("Initial name: " + person.getName()); Explained: getName() and setName(), which encapsulate the functionality of getting and setting the name of the person.

Time-complexity (of an algorithm)

How fast an algorithm runs, expressed as a function of the number of input values.

Space-complexity (of an algorithm)

How much memory an algorithm needs.

How data access is restricted between stacks, queues, and deques.

In stacks, data access is restricted to only the top element of the stack, which is the last item added. In queues, data access is restricted to only the front element of the queue, which is the first item added. In deques (double-ended queues), data access is restricted to both the front and back elements, allowing for insertion and deletion from both ends of the deque.

Inheritance

Inheritance refers to the ability of a class to inherit properties and behaviors from a parent class. Example- An "is a" relationship A truck is a vehicle. While a vehicle is not a truck.

Last in, First out (LIFO)

Last in, First out (LIFO) is a principle used in computer science and other fields to describe a system or structure where the last item added to the system or structure is the first item to be removed. (A STACK)

The drawbacks to using Recursion

Limited memory: Recursion can consume a lot of memory, especially if the depth of recursion is large or if the function creates new objects or data structures on each call. Performance overhead: Recursive functions can be slower and less efficient than iterative functions due to the overhead of function calls and stack management.

Linked nodes (AKA Chained nodes)

Linked nodes are a fundamental part of a linked data structure that stores data as a series of connected nodes, where each node contains a data element and a reference to the next node in the sequence.

Mechanisms of data access

Mechanisms of data access refer to the ways in which data is accessed and manipulated in a data structure.

Overriding methods

Method overriding refers to the ability of a subclass to provide its own overridden implementation of a method that is already defined in the parent class. Example- We have two classes: A child class Boy and a parent class Human. The Boy class extends Human class. Both the classes have a common method void eat() . Boy class is giving its own implementation to the eat() method or in other words it is overriding the eat() method.

Polymorphism

Polymorphism refers to the ability of objects to take on multiple forms or behaviors, depending on their context. Example-

Pros and cons of contiguous storage mechanism

Pros: Contiguous memory allows for constant time access to any element in the structure, making it ideal for random access of elements. It is easy to implement and efficient for storing large blocks of data that can be accessed in a random manner. Since the memory locations for each element are adjacent to each other, it allows for efficient use of CPU caches and reduces cache misses, leading to faster access times. Cons: Contiguous memory can lead to memory fragmentation, which can limit the ability to dynamically resize the data structure and cause inefficient memory usage. Insertion or deletion of elements in the middle of the structure requires moving all subsequent elements, which can be inefficient for large data structures. Contiguous memory does not support efficient insertion or deletion operations at arbitrary positions. This means that adding or removing elements from the middle of the structure can be slow and require a lot of memory movement.

Pros and cons of linked based storage mechanism

Pros: Linked-based storage mechanism allows for efficient insertion and deletion operations at arbitrary positions in the structure. Linked structures can be dynamically resized with minimal overhead, as new nodes can be easily added or removed. Linked structures can avoid memory fragmentation, as nodes can be scattered throughout memory. Cons: Linked structures can have higher overhead than contiguous structures, as each node requires additional memory to store the pointers to other nodes. Linked structures require sequential access to elements, which can make random access inefficient. Linked structures are more complex to implement and maintain than contiguous structures.

Sorting in increasing order vs. sorting in decreasing order.

Sorting in increasing order, also known as ascending order, arranges the elements of a list or array in order from smallest to largest, where the first element is the smallest and the last element is the largest. Sorting in decreasing order, also known as descending order, arranges the elements of a list or array in order from largest to smallest, where the first element is the largest and the last element is the smallest.

Know which of the sorts discussed are stable and which are unstable.

Stable sorts: Insertion sort, Merge sort, Radix sort Unstable sorts: Quick sort, Selection sort

Know the BigO of adding and removing from a stack, queue, and deque for both array based and linked based data structures.

Stack: Array-based implementation :Adding an element: O(1) Removing an element: O(1) Linked list-based implementation: Adding an element: O(1) Removing an element: O(1) Queue: Array-based implementation: Adding an element: O(1) Removing an element: O(1) Linked list-based implementation: Adding an element: O(1) Removing an element: O(1) Deque: Array-based implementation: Adding an element at front or back: O(1) Removing an element from front or back: O(1) Linked list-based implementation: Adding an element at front or back: O(1) Removing an element from front or back: O(1)

How to remove from a stack, queue, and deque.

Stack: To remove an element from a stack, you use the pop() operation, which removes and returns the element at the top of the stack. Queue: To remove an element from a queue, you use the dequeue() operation, which removes and returns the element at the front of the queue. Deque: To remove an element from a deque, you can use either the pop() or popleft() operations, depending on whether you want to remove the element from the right or left end of the deque, respectively.

The algorithm for a linear search.

The binary search algorithm reduces the search space in half with each iteration by comparing the target value with the middle element of the sorted list. The algorithm terminates when the target value is found, or the search space is reduced to an empty range, indicating that the target value is not present in the list.

Base case

The condition under which a recursive function returns without calling itself, thereby ending the recursive calls.

The algorithm for the insertion sort.

The insertion sort algorithm works by iterating over the list and inserting each element in its correct position within a sorted sub-list. The sorted sub-list starts with the first element and grows with each iteration.

Conditions which must be met for a binary search to be possible.

The list must be sorted in ascending or descending order. The list must be finite, meaning it has a definite end or length. The elements of the list must be comparable or have a defined ordering relationship. The search algorithm must have access to the middle element of the list or the ability to compute its index.

The algorithm for the merge sort.

The merge sort algorithm works by recursively dividing the list into halves, sorting each half, and then merging the sorted halves into a single sorted list.

Dynamic Binding

The process that enables different objects to - Use different method actions - For the same method name

The algorithm for the quick sort.

The quick sort algorithm works by selecting a pivot element from the list, partitioning the list into two sub-lists around the pivot, and recursively applying quick sort to each sub-list.

The algorithm for the radix sort.

The radix sort algorithm sorts a list of integers by grouping them according to their individual digits.

The algorithm for the selection sort.

The selection sort algorithm works by repeatedly finding the minimum element from the unsorted portion of the list and swapping it with the first unsorted element. The algorithm then moves the boundary of the unsorted portion of the list one element to the right and repeats the process until the entire list is sorted.

How to add to a stack, queue, and deque.

To add an element to a stack, you push it onto the top of the stack. To add an element to a queue, you enqueue it at the back of the queue. To add an element to a deque, you can either push it onto the front or back of the deque, depending on the specific implementation.

Stack Overflow Error

When Java runs out of room in its stack, typically because a recursive program never ended.

Composition

When a class has a data field that is an instance of another class Example - an object of type Student.

Overloading Methods

When the derived class method has: The same name, The same return type, but- Different number or type of parameters Example- function add(int a, int b); function add(float a, float b);

BigO for the worst, best, and average case scenarios when using the radix sort.

Worst case: O(d*(n+k)), where d is the number of digits in the largest number, n is the number of elements in the list, and k is the range of values (i.e., the number of distinct possible digits). This occurs when all the numbers in the list have the same number of digits and the algorithm needs to sort them based on each digit. Best case: O(d*(n+k)), where d is the number of digits in the largest number, n is the number of elements in the list, and k is the range of values. This occurs when the list is already sorted or nearly sorted based on the digits being used. Average case: O(d*(n+k)), where d is the number of digits in the largest number, n is the number of elements in the list, and k is the range of values. This occurs when the list is randomly ordered and needs to be sorted based on each digit.

BigO for the worst, best, and average case scenarios when using the merge sort.

Worst case: O(n*log n), where n is the number of elements in the list. This occurs when the merge sort algorithm needs to split the list into its smallest possible sublists before merging them back together in sorted order. Best case: O(n*log n), where n is the number of elements in the list. This occurs when the merge sort algorithm can divide the list into sublists of roughly equal size, reducing the number of comparisons needed during the merging process. Average case: O(n*log n), where n is the number of elements in the list. This occurs when the list is randomly ordered and needs to be divided into sublists before being merged back together in sorted order.

BigO for the worst, best, and average case scenarios when using the insertion sort.

Worst case: O(n^2), where n is the number of elements in the list. This occurs when the list is in reverse sorted order, and each element needs to be compared with every other element before it is finally placed in the correct position. Best case: O(n), where n is the number of elements in the list. This occurs when the list is already sorted, and the insertion sort algorithm simply needs to iterate through the list once to verify that it is sorted. Average case: O(n^2), where n is the number of elements in the list. This occurs when the list is randomly ordered, and each element needs to be compared with every other element before it is finally placed in the correct position.

BigO for the worst, best, and average case scenarios when using the selection sort.

Worst case: O(n^2), where n is the number of elements in the list. This occurs when the list is in reverse sorted order, and each element needs to be compared with every other element before it is finally placed in the correct position. Best case: O(n^2), where n is the number of elements in the list. This occurs when the list is already sorted, but the selection sort algorithm still needs to compare each element with every other element in the list to ensure that it is in the correct position. Average case: O(n^2), where n is the number of elements in the list. This occurs when the list is randomly ordered, and each element needs to be compared with every other element before it is finally placed in the correct position.

BigO for the worst, best, and average case scenarios when using the quick sort.

Worst case: O(n^2), where n is the number of elements in the list. This occurs when the pivot chosen by the quick sort algorithm consistently divides the list into unbalanced sublists, causing the algorithm to recursively sort the same sublist multiple times. Best case: O(n*log n), where n is the number of elements in the list. This occurs when the pivot chosen by the quick sort algorithm consistently divides the list into balanced sublists, reducing the number of comparisons needed during the sorting process. Average case: O(n*log n), where n is the number of elements in the list. This occurs when the pivot chosen by the quick sort algorithm divides the list into roughly equal sublists.

BigO for the worst, best, and average case scenarios when searching through an unsorted or sorted list of data using the linear search.

Worst-case scenario: In the worst case, linear search has to traverse through all the elements of the list to find the target element, which takes O(n) time complexity, where n is the number of elements in the list. Best-case scenario: In the best case, the target element is located at the beginning of the list, so linear search needs only one comparison, which takes O(1) time complexity. Average-case scenario: In the average case, the target element is equally likely to be at any position in the list. On average, the linear search needs to compare half of the elements in the list before finding the target element, which takes O(n/2) time complexity, but we can simplify it to O(n) as n grows large.

BigO for the worst, best, and average case scenarios when search through a sorted list of data using a binary search.

Worst-case scenario: In the worst case, the target element is not present in the list, and binary search will have to examine every element in the list to determine this. This takes O(log n) time complexity, where n is the number of elements in the list. Best-case scenario: In the best case, the target element is located at the middle of the list, and binary search finds it in the first comparison. This takes O(1) time complexity. Average-case scenario: In the average case, binary search needs to make log n comparisons to find the target element in a list of n elements, as it divides the search space in half with each comparison. This takes O(log n) time complexity.

The worst, best, and average case scenarios for searching for an element in a collection.

Worst-case scenario: The scenario where an algorithm takes the longest time to complete, typically when the input data is the least favorable or the algorithm encounters the most challenging conditions. Best-case scenario: The scenario where an algorithm takes the shortest time to complete, typically when the input data is the most favorable or the algorithm encounters the easiest conditions. Average-case scenario: The scenario where an algorithm takes an expected or average amount of time to complete, typically when the input data is random or uniformly distributed.

Abstract Data Type

a description of operations on a data type that could have multiple possible implementations.

Recursive case

a condition of the input data where the data will be handled by call(s) to the same program.

Generics (Types)

classes, interfaces, and methods that have placeholders (type parameters) for one or more of the types that they store or use Example- public static <T> T getFirstElement(T[] array) { Integer firstInt = getFirstElement(intArray);

Direct Recursion

when a function directly calls itself

Indirect Recursion

when function A calls function B, which in turn calls function A


संबंधित स्टडी सेट्स

Chapter 6: The Nature of Management

View Set

Religion final: CH 4,5,6 and Tests

View Set

Joining Data from Multiple Tables

View Set

Physics II - Geometric Optics & Reflection

View Set