80 likes | 209 Views
In this lecture, we explore the MergeSort algorithm, which sorts a given array of numbers in O(n log n) time. The input is an unsorted array, and the output is the array in sorted order. We'll discuss the base case for two elements, the recursive division of the array into left and right halves (aL and aR), and the merging process. Additionally, important reminders include submitting homework by 1:10 PM on Fridays and attending extra question sessions next week. Get ready to solve recurrences and delve deeper into the efficiency of MergeSort!
E N D
Lecture 29 CSE 331 Nov 11, 2009
To be strictly enforced For the rest of the semester on Fridays SUBMIT your HOMEWORKS by 1:10 PM
Extra Q sessions next week Monday and Tuesday 4-6pm CSE 242
Mergesort algorithm Input: a1, a2, …, an Output: Numbers in sorted order MergeSort( a, n ) If n = 2 return the order min(a1,a2); max(a1,a2) aL = a1,…, an/2 aR = an/2+1,…, an return MERGE ( MergeSort(aL, n/2), MergeSort(aR, n/2) )
An example run 1 51 51 1 19 100 100 19 2 2 8 8 4 3 3 4 1 8 1 2 19 19 3 2 4 51 51 3 100 4 8 100 MergeSort( a, n ) If n = 2 return the order min(a1,a2); max(a1,a2) aL = a1,…, an/2 aR = an/2+1,…, an return MERGE ( MergeSort(aL, n/2), MergeSort(aR, n/2) )
Correctness Input: a1, a2, …, an Output: Numbers in sorted order By induction on n MergeSort( a, n ) If n = 2 return the order min(a1,a2); max(a1,a2) aL = a1,…, an/2 aR = an/2+1,…, an return MERGE ( MergeSort(aL, n/2), MergeSort(aR, n/2) ) Inductive step follows from correctness of MERGE
Today’s agenda Show that Mergesort runs in O(n log n) time Solve recurrences
To be strictly enforced For the rest of the semester on Fridays SUBMIT your HOMEWORKS by 1:10 PM