2.1 Merge Sort Flashcards
What is Divide and Conquer?
Divide: an instance into smaller instances of the same problem
Conquer: by recursively solving the sub-instances until we can solve directly
Combine: the partial solutions to the solution for the original instance
What is recursion
A function that calls itself
When combining left and right part of array, what are the formulas for n1 and n2? (the lengths of the two arrays)
n1 = m - L + 1
n2 = r - m
What is a sentinel
Usually infinity, used to compare to the biggest number in the array. So the biggest number in the array is compared and then goes at the end.
How many comparisons does Merge make?
r - L + 1 (n)
What techniques are used to prove correctness of MergeSort?
Using proof by induction
Can merge sort be done in situ (using only the array you were given and no extra space)
No
Is MergeSort stable? If there are 2 1’s in the array will the first 1 always stay before the second 1?
Yes
Pros and Cons of merge sort
Pro:
Divide and Conquer
Good ‘worst case’ running time O(nlog(n))
Con:
Not in situ (needs an extra n space)