Algorithms Flashcards
What is insertion sort?
The natural question is, how can we place into our array while still keeping it sorted?
This operation is known as an insertion
What are the steps for insertion sort?
Place x at the end of the array.
Compare to the element on its left. There are three possibilities:
If there is no element to the left, then we are done, since x will be the smallest element and it is already at the start of the array.
If x is greater than or equal to it, then we are done; x is on the right of all smaller elements and on the left of all larger elements, so the array is once again sorted.
If x is smaller, then it should be before the other element. Switch the two to put x on front.
Repeat step two until finished.
What are the steps to the FULL insertion sort algorithm?
Designate the leftmost element of the array as the only element of the sorted side. This side is guaranteed to be sorted by default, since it now contains only one element.
Insert the first element of the unsorted side into the sorted side, increasing the number of sorted elements by one.
Repeat step two until there are no unsorted elements left.
What must be tracked during a full insertion sort?
All we have to do is keep track of how much of the original array is sorted.
What is the formula for the number of comparisons in the worst case for an array?
n( n - 1 ) / 2
What is the formula for the number of comparisons in the best case for an array?
n - 1
What is the goal of the divide and conquer method?
The goal of this method is to
divide a problem into equal sized sub-problems recursively;
conquer each problem separately, solving them recursively;
combine results.
What are the three steps of a merge sort algorithm?
1) Divide: Split the list into two (approximately) equally sized lists.
2) Conquer: Sort each of the two lists separately (using mergesort itself!).
3) Combine: Given two sorted lists of approximately the same size, merge them into one big sorted list.
What is the code for mergesort?
def merge(left, right):
result = []
left_idx, right_idx = 0, 0
while left_idx < len(left) and right_idx < len(right):
# change the direction of this comparison to change the direction of the sort
if left[left_idx] <= right[right_idx]:
result.append(left[left_idx])
left_idx += 1
else:
result.append(right[right_idx])
right_idx += 1
if left: result.extend(left[left_idx:]) if right: result.extend(right[right_idx:]) return result
def merge_sort(m): if len(m) <= 1: return m
middle = len(m) // 2 left = m[:middle] right = m[middle:]
left = merge_sort(left) right = merge_sort(right) return list(merge(left, right))
What is the advantage of mergesort?
The mergesort algorithm takes advantage of the fact that two sorted lists can be merged into one sorted list very quickly.
What is the best runtime on average?
It turns out that the runtime of O(NlogN) is the best we can do, on average, for a comparison-based sorting algorithm.
What is Quicksort?
Quicksort, just like mergesort, is a “divide-and-conquer” sorting algorithm that runs in O(NlogN) time in the average case.
What is the worst case for Quicksort?
Although it has a worst case runtime of O(n**2), overall quicksort tends to outperform mergesort in the real world, making it an attractive choice for many programmers.
What is partitioning in Quicksort?
Pick a pivot element (typically the last item in the array). All numbers lower than this value go to the left and all elements higher to the right. For quicksort, this step is commonly known as partitioning.
What is the key to Quicksort?
The key to a successful sort is picking the right pivot point.