Sorts + Searches + Big-O Flashcards
Big-O of Selection Sort (best case)
O(n^2)
Big-O of Selection Sort (average case)
O(n^2)
Big-O of Selection Sort (worst case)
O(n^2)
Big-O of Insertion Sort (best case)
O(n)
Big-O of Insertion Sort (average case)
O(n^2)
Big-O of Insertion Sort (worst case)
O(n^2)
Big-O of Merge Sort (best case)
O(nlogn)
Big-O of Merge Sort (average case)
O(nlogn)
Big-O of Merge Sort (worst case)
O(nlogn)
Big-O of Quick Sort (best case)
O(nlogn)
Big-O of Quick Sort (average case)
O(nlogn)
Big-O of Quick Sort (worst case)
O(n^2)
Big-O of Linear Search (best case)
O(1)
Big-O of Linear Search (average case)
O(n)
Big-O of Linear Search (worst case)
O(n)
Big-O of Binary Search (best case)
O(1)
Big-O of Binary Search (average case)
O(logn)
Big-O of Binary Search (worst case)
O(logn)
How does selection sort work?
Selection sort finds the largest item in the array, and swaps it to the end. The item that was previously at the end is now where the largest item used to be. Repeat with an array of length - 1.
How does insertion sort work?
Insertion sort checks if the next item in the array is smaller than the item at the current index. If it is, it is moved to before the current index, and it checks again. IT DOES NOT SWAP!!
How does merge sort work?
Merge sort splits the data in half until all that’s left are pairs. It then sorts the first two pairs, then combines them, then sorts them again. Then it moves on to the next two pairs and does the same thing. Then it combines both and sorts all 8 numbers, and so on.
How does quick sort work?
Quick sort selects a pivot, which is the first item in the array. Then it moves from the left and from the right of the array until it finds two items that are bigger on the left and smaller on the right. It swaps them. Once the right pointer is less than the left pointer, the pivot and the item at the right pointer are swapped. The next pivot is chosen, and so on.
How does linear search work?
Linear search uses one for loop to go through the entire array and check each item until it finds what it wants.
How does binary search work?
Binary search compares the target to the middle item of the array. If it’s the same, it returns that location. If not, it either checks the top or bottom half of the array depending on how the target was in comparison to the middle, and recurs. It continues until the index finds the target or the first pointer is greater than the last.