1 / 42

Lecture 24: Merge Sort –or– Lessons from Roman Empire

CSC 213 – Large Scale Programming. Lecture 24: Merge Sort –or– Lessons from Roman Empire. Today’s Goals. Review past discussion of data sorting algorithms Weaknesses of past approaches & when we use them Can we find limit to how long sorting needs?

lael
Download Presentation

Lecture 24: Merge Sort –or– Lessons from Roman Empire

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 213 – Large Scale Programming Lecture 24:Merge Sort –or–Lessons from Roman Empire

  2. Today’s Goals • Review past discussion of data sorting algorithms • Weaknesses of past approaches & when we use them • Can we find limit to how long sorting needs? • What does this mean for sorting & those past sorts • Get good idea of how merge sort is executed • What is algorithm and what will this require? • What are execution trees & how they show runtime?

  3. Ghosts of Sorts Past • We have already seen & discussed 4 sorts • Bubble-sort-- O(n2) time sort; slowest sort • Selection-sort-- O(n2) time sort; PQ concept • Insertion-sort-- O(n2) time sort; PQ concept • Heap-sort-- O(n log n) time sort; requires PQ • All of these sorts of limited usefulness

  4. Ghosts of Sorts Past • We have already seen & discussed 4 sorts • Bubble-sort-- O(n2) time sort; slowest sort • Selection-sort-- O(n2) time sort; PQ concept • Insertion-sort-- O(n2) time sort; PQ concept • Heap-sort-- O(n log n) time sort; requires PQ • All of these sorts of limited usefulness

  5. Counting Comparisons • Consider sort as a path in a decision tree • Nodes are single decision needed for sorting Is xi > xj ? yes no

  6. Counting Comparisons • Consider sort as a path in a decision tree • Nodes are single decision needed for sorting • Traveling from root to leaf sorts data • Tree’s height is lower-bound on sorting complexity

  7. Decision Tree Height • Unique leaf for each ordering of data initially • Needed to ensure we sort different inputs differently • Consider 4, 5as data to be sorted using a tree • Could be entered in 2 possible orders: 4, 5or5, 4 • Need two leaves for this sort unless (4 < 5) == (5 < 4)

  8. Decision Tree Height • Unique leaf for each ordering of data initially • Needed to ensure we sort different inputs differently • Consider 4, 5as data to be sorted using a tree • Could be entered in 2 possible orders: 4, 5or5, 4 • Need two leaves for this sort unless (4 < 5) == (5 < 4) • For sequence of n numbers, can arrange n! ways • Tree with n! leaves needed to sort n numbers • Given this many leaves, what is height of the tree?

  9. Decision Tree Height • With n! external nodes, binary tree’s height is:

  10. The Lower Bound • But what does O(log(n!))equal?n! = n * n-1 * n -2 * n-3 * n/2 * … * 2 * 1n!≤(½*n)½*n(½of series is larger than ½*n) log(n!)≤log((½*n)½*n) log(n!)≤½*n* log(½*n) O(log(n!)) ≤O(½*n * log(½*n))

  11. The Lower Bound • But what does O(log(n!))equal?n! = n * n-1 * n -2 * n-3 * n/2 * … * 2 * 1n!≤(½*n)½*n(½of series is larger than ½*n) log(n!)≤log((½*n)½*n) log(n!)≤½*n* log(½*n) O(log(n!)) ≤O(½*n * log(½*n))

  12. The Lower Bound • But what does O(log(n!))equal?n! = n * n-1 * n -2 * n-3 * n/2 * … * 2 * 1n!≤(½*n)½*n(½of series is larger than ½*n) log(n!)≤log((½*n)½*n) log(n!)≤½*n* log(½*n) O(log(n!)) ≤O(n log n)

  13. Lower Bound on Sorting • Smallest number of comparisonsis tree’s height • Decision tree sorting n elements has n! leaves • At least log(n!) height needed for this many leaves • As we saw, this simplifies to at most O(n log n) height • O(n log n) time needed to compare data! • Means that heap-sort is fastest possible (in big-Oh) • Pain-in-the- to code & requires external heap

  14. Lower Bound on Sorting • Smallest number of comparisonsis tree’s height • Decision tree sorting n elements has n! leaves • At least log(n!) height needed for this many leaves • As we saw, this simplifies to at most O(n log n) height • O(n log n) time needed to compare data! • Means that heap-sort is fastest possible (in big-Oh) • Pain-in-the- to code & requires external heap • Is there a simple sort using only Sequence?

  15. Julius, Seize Her! • Formula to Roman success • Divide peoples before an attack • Then conquer weakened armies • Common programming paradigm • Divide: split into 2 partitions • Recur:solve for partitions • Conquer:combine solutions

  16. Divide-and-Conquer • Like all recursive algorithms, need base case • Has immediate solution to a simple problem • Work is not easy and sorting 2+ items takes work • Already sorted 1 item since it cannot be out of order • Sorting a list with 0 items even easer • Recursive step simplifies problem & combines it • Begins by splitting data into two equal Sequences • Merges subSequencesafter they have been sorted

  17. Merge-Sort AlgorithmmergeSort(Sequence<E> S, Comparator<E>C) ifS.size() <2then // Base case return S else // Recursive case //Split Sinto two equal-sized partitionsS1andS2 mergeSort(S1,C) mergeSort(S2,C) S merge(S1,S2,C) returnS

  18. Merging Sorted Sequences Algorithmmerge(S1,S2,C)Sequence<E> retVal= //Code instantiating a Sequencewhile!S1.isEmpty() && ! S2.isEmpty()ifC.compare(S1.get(0),S2.get(0)) < 0retVal.insertLast(S1.removeFirst())elseretVal.insertLast(S2.removeFirst())endifendwhile!S1.isEmpty()retVal.insertLast(S1.removeFirst())endwhile!S2.isEmpty() retVal.insertLast(S2.removeFirst()) endreturn retVal

  19. Execution Tree • Depicts divide-and-conquer execution • Recursive call represented by each oval node • Original Sequence shown at start • At the end of the oval, sorted Sequence shown • Initial call at root of the (binary) tree • Bottom of the tree has leaves for base cases

  20. Execution Example • Not in a base case 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9

  21. Execution Example • Not in a base case, so split into S1 & S2 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9

  22. Execution Example • Not in a base case, so split into S1 & S2 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9

  23. Execution Example • Recursively call merge-sort on S1 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9

  24. Execution Example • Recursively call merge-sort on S1 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9

  25. Execution Example • Not in a base case, so split into S1 & S2 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9

  26. Execution Example • Not in a base case, so split into S1 & S2 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9

  27. Execution Example • Recursively call merge-sort on S1 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9

  28. Execution Example • Recursively call merge-sort on S1 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9 7 2  2 7

  29. Execution Example • Still no base case, so split again & recurse on S1 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9 72 2 7 7 7

  30. Execution Example • Enjoy the base case – literally no work to do! 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9 72 2 7 77

  31. Execution Example • Recurse on S2and solve for this base case 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9 72 2 7 77 22

  32. Execution Example • Merge the two solutions to complete this call 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9 72 2 7 77 22

  33. Execution Example • Recurse on S2and sort this subSequence 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9 72 2 7 9 4  4 9 77 22

  34. Execution Example • Split into S1 & S2and solve the base cases 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9 72 2 7 94 4 9 77 22 9 9 44

  35. Execution Example • Merge the 2 solutions to sort this Sequence 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4  2 4 7 9 72 2 7 944 9 77 22 9 9 44

  36. Execution Example • I feel an urge, an urge to merge 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 72 2 7 944 9 77 22 9 9 44

  37. Execution Example • Let's do the merge sort again! (with S2) 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 11 3 6 8 72 2 7 944 9 77 22 9 9 44

  38. Execution Example • Let's do the merge sort again! (with S2) 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 11 3 6 8 72 2 7 944 9 38 3 8 77 22 9 9 44 33 88

  39. Execution Example • Let's do the merge sort again! (with S2) 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 11 3 6 8 72 2 7 944 9 38 3 8 611 6 77 22 9 9 44 33 88 66 11

  40. Execution Example • Let's do the merge sort again! (with S2) 7 2 9 4 3 8 6 1  1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 11 3 6 8 72 2 7 944 9 38 3 8 611 6 77 22 9 9 44 33 88 66 11

  41. Execution Example • Merge the last call to get the final result 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 11 3 6 8 72 2 7 944 9 38 3 8 611 6 77 22 9 9 44 33 88 66 11

  42. For Next Lecture • New weekly assignment for week was posted • Discussing sorts which have few concepts to code • Will return soon enough; do not worry about it • Keep reviewing requirements for program #2 • Preliminary deadlines arrive before final version • Time spent on requirements & design saves coding • Reading on quick sort for this Friday • Guess what? It can be really, really, quick • Severe drawbacks also exist; read to understand this

More Related