SLIDE 1
Announcements: PA1 available, due 01/28, 11:59p.
HW2 out soon, due 10/04, 11:59p. TODAY: Merge Sort, general sorting discussion
A “divide and conquer” algorithm.
- 1. If the array has 0 or 1 elements, it’s sorted. Stop.
- 2. Split the array into two approximately equal-sized halves.
- 3. Sort each half recursively
- 4. Merge the sorted halves to produce one sorted result.
RT:
1 2 3 4 5 6 7 void mergeSort(vector<T> & A, int lo, int hi){ if (hi > lo) { int mid = (hi + lo)/2; mergeSort(A, lo, mid); mergeSort(A, mid+1, hi); merge(A, lo, mid, hi); } }