CSE116 / CSE504 Introduction to Computer Science II Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Thursday 12:00 PM – 2:00 PM Friday 8:30 AM – 10:30 AM OR request appointment via e-mail PROFESSIONALISM Turn off and put away electronics: cell phones pagers laptops tablets etc. © Dr. Carl Alphonce Today Sorting / Merge Sort ROADMAP Coming up Heaps / Priority queues © Dr. Carl Alphonce channel 1 Ifselectionsortispresentedwiththisinput, 12 27 16 84 72 38 10 65 whichoftheseisapossibleintermediateconfigurationof thearray? 1) 10 27 16 84 72 38 12 65 2) 10 12 16 84 72 38 27 65 3) 10 12 16 27 38 84 72 65 A. B. C. D. E. (1)only (2)only (1)and(2)only (2)and(3)only (1),(2)and(3) Ifselectionsortispresentedwiththisinput, 12 27 16 84 72 38 10 65 whichoftheseisapossibleintermediateconfigurationof thearray? 1) 10 27 16 84 72 38 12 65 2) 10 12 16 84 72 38 27 65 3) 10 12 16 27 38 84 72 65 A. B. C. D. E. (1)only (2)only (1)and(2)only (2)and(3)only (1),(2)and(3) Ifselectionsortispresentedwiththisinput, 12 27 16 84 72 38 10 65 whichoftheseisapossibleintermediateconfigurationof thearray? 1) 10 27 16 84 72 38 12 65 2) 10 12 16 84 72 38 27 65 3) 10 12 16 27 38 84 72 65 A. B. C. D. E. (1)only (2)only (1)and(2)only (2)and(3)only (1),(2)and(3) SORTING O(n log(n)) sorting algorithm: MERGE SORT © Dr. Carl Alphonce Merge sort Basic idea input is presented in an array the array is split into two partitions, of (roughly) equal size each partition is recursively merge sorted partitions are sorted, they are merged to give answer to original problem 12 27 16 84 72 38 10 65 12 split 12 27 27 16 16 84 84 72 72 38 38 10 10 65 65 recursively merge sort the partitions 12 27 16 84 72 38 10 65 12 27 16 84 72 38 10 65 12 16 27 84 10 38 65 72 merge the sorted partitions 12 27 16 84 72 38 10 65 12 27 16 84 72 38 10 65 12 16 27 84 10 38 65 72 10 12 16 27 38 65 72 84 the whole process 12 27 16 84 72 38 10 65 12 27 16 84 72 38 10 65 12 27 16 84 72 38 10 65 27 16 84 72 38 10 12 27 16 84 38 72 10 65 12 16 27 84 10 38 65 72 12 10 12 16 27 38 65 72 65 84 efficiency? 12 27 16 84 72 38 10 65 12 27 16 84 72 38 10 65 12 27 16 84 72 38 10 65 27 16 84 72 38 10 12 27 16 84 38 72 10 65 12 16 27 84 10 38 65 72 12 10 12 16 27 38 65 72 65 84 efficiency? 12 27 16 84 72 38 10 The work of a sorting algorithm is measured by the number of comparisons, x<y, that it makes. 65 12 27 16 84 72 38 10 65 12 27 16 84 72 38 10 65 27 16 84 72 38 10 27 16 84 38 72 10 12 12 12 10 16 12 27 16 84 27 10 38 38 65 65 72 65 65 72 84 Merge sort does comparisons only in the merge phase (bottom half of diagram). During merge, all n positions of the merged array need to be determined. One comparison is needed for each. So, at each level O(n) work is needed. How many levels are there in the bottom half? log2 n! Therefore the runtime is O(n log n). efficiency? 12 27 16 84 72 38 10 65 12 27 16 84 72 38 10 65 12 27 16 84 72 38 10 65 27 16 84 72 38 10 12 27 16 84 38 72 10 65 12 16 27 84 10 38 65 72 12 10 12 16 27 38 65 72 65 84 What about upper half of diagram? Splitting takes very little time. Even if we overestimate and think that O(n) is needed at each level, the top half would only contribute O(n log n). Adding top and bottom halves would yield: O(n log n + n log n) = O(2 n log n) = O(n log n) since constants are absorbed into the notation. O(n log n) No general purpose sorting algorithm can do better. How about in practice? empirical testing efficiency 12 27 16 84 72 38 10 65 10 27 16 84 72 38 12 65 10 12 16 84 72 38 27 65 10 12 16 84 72 38 27 65 10 12 16 27 72 38 84 65 10 12 16 27 38 72 84 65 10 12 16 27 38 65 84 72 10 12 16 27 38 65 72 84 10 12 16 27 38 65 72 84 Maybe we will get a better bound by computing n+(n-1)+(n-2)+…+1 or Σ i, for i=1…n which is n*(n+1)/2 which is still O(n2) efficiency 12 27 16 84 72 38 10 12 27 16 84 38 72 10 65 12 16 27 84 10 38 65 72 10 12 16 27 38 65 72 65 84 What about upper half of diagram? Splitting takes very little time. Even if we overestimate and think that O(n) is needed at each level, the top half would only contribute O(n log n). Adding top and bottom halves would yield: O(n log n + n log n) = O(2 n log n) = O(n log n) since constants are absorbed into the notation.