1 / 38

Lecture 25: Quick Sort

or. CSC 213 – Large Scale Programming. Lecture 25: Quick Sort. Today’s Goals. Begin by discussing basic approach of quick sort Divide-and-conquer used , but how does this help? Merge sort also divides-and-conquers, what differs? Show execution tree & discuss meaning for quick sort

mikaia
Download Presentation

Lecture 25: Quick Sort

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. or CSC 213 – Large Scale Programming Lecture 25:Quick Sort

  2. Today’s Goals • Begin by discussing basic approach of quick sort • Divide-and-conquer used, but how does this help? • Merge sort also divides-and-conquers, what differs? • Show execution tree & discuss meaning for quick sort • Consider selection and important of the pivot • Quick sort rocks for which pivots and why? • If we get really unlucky, when would quick sort suck? • How can we affect choice to insure we get lucky • Given all these facts: is quick sort worth the risk?

  3. Quick (ungraded) Quiz • For what cases does quick sort suck? • When would quick sort execute fastest? • How does quick sort use divide-and-conquer?

  4. 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 Sequences • Combine subSequencesonce they are sorted

  5. Quick Sort • Divide: Partition by pivot • L has values <= p • G uses values >= p • Recur: Sort L and G • Conquer: Merge L, p, G p

  6. Quick Sort • Divide: Partition by pivot • L has values <= p • G uses values >= p • Recur: Sort L and G • Conquer: Merge L, p, G p

  7. Quick Sort • Divide: Partition by pivot • L has values <= p • G uses values >= p • Recur: Sort L and G • Conquer: Merge L, p, G p p G L

  8. Quick Sort • Divide: Partition by pivot • L has values <= p • G uses values >= p • Recur: Sort L and G • Conquer: Merge L, p, G p p G L p

  9. Quick Sort v. Merge Sort Quick Sort Merge Sort • Work mostly splitting data • Cannot guarantee even split • Should skip some comparisons • Does not need extra space • Less work allocating arrays • Blindly merges all the data • Data already in sorted order! • Blindly splits data in half • Always gets even split • Needs* to use other arrays • Wastes time in allocation • Complex workaround exists • Work mostly in merge step • Combines two (sorted) halves • Always skips some comparisons

  10. 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

  11. Execution Example • Each call starts by selecting the pivot 7 2 9 4 3 7 6 1  1 2 3 4 6 7 7 9

  12. Execution Example • Each call starts by selecting the pivot 7 2 9 4 3 7 61 1 2 3 4 6 7 7 9

  13. Execution Example • Split into L & Gpartitions around pivot 7 2 9 4 3 7 6 1  1 2 3 4 6 7 7 9

  14. Execution Example • Split into L &Gpartitions around pivot 7294 3761 1 2 3 4 6 7 7 9

  15. Execution Example • Recursively solve L partition first 7294 3761 1 2 3 4 6 7 7 9

  16. Execution Example • Recursively solve L partition first 7294 3761 1 2 3 4 6 7 7 9 2 4 3 1 1 2 3 4

  17. Execution Example • Select new pivotand split into L &Gpartitions 7294 3761 1 2 3 4 6 7 7 9 24 3 1 1 2 3 4

  18. Execution Example • Recursively solve for L 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 11

  19. Execution Example • Recursively solve for L & enjoy base case 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 11

  20. Execution Example • Now solve for Gpartition and select pivot 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 11 43 3 4

  21. Execution Example • Recursively solve for L 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 11 43 3 4

  22. Execution Example • Recursively solve for L & enjoy base case 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 11 43 3 4

  23. Execution Example • Now solve for G& enjoy base case 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 11 43 3 4 44

  24. Execution Example • Add L,pivot,&Gto complete previous call 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 11 433 4 44

  25. Execution Example • Add L,pivot,&Gto complete previous call 7294 3761 1 2 3 4 6 7 7 9 24 3 1 1 2 3 4 11 433 4 44

  26. Execution Example • Recursively sort Gfrom original call 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 11 433 4 44

  27. Execution Example • Recursively sort Gfrom original call 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 7 9 7 7 7 9 11 433 4 44

  28. Execution Example • Select pivot& partition intoL & G 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 7 9 7 7 7 9 11 433 4 44

  29. Execution Example • SolveLrecursively via base case & move to G 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 7 9 7 7 7 9 11 433 4 7 9 7 9 44

  30. Execution Example • Select pivot& partition intoL & G 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 7 9 7 7 7 9 11 433 4 797 9 44

  31. Execution Example • Solve through two base cases in L&G 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 7 9 7 7 7 9 11 433 4 797 9 44 99

  32. Execution Example • Add L,pivot,&Gto complete the call 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 7 9 7 7 7 9 11 433 4 797 9 44 99

  33. Execution Example • Add L,pivot,&Gto complete the call 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 7 9 77 7 9 11 433 4 797 9 44 99

  34. Execution Example • Add L,pivot,&Gto complete final call 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 7 9 77 7 9 11 433 4 797 9 44 99

  35. Execution Example • Sorts data, but only as good as pivotchoice 7294 3761 1 2 3 4 6 7 7 9 24 3 1  1 2 3 4 7 9 77 7 9 11 433 4 797 9 44 99

  36. Quick-Sort’s Pivot • Can select any element as pivot • Pivot's existence required only, not where or what it is • Quick-Sort's ideal pivot is median element • Equal-sizedL&Gwhen median element selected • Makes complexity equal to Merge-Sort • Must sort data first to find the median, however • Smallest (or largest element) worst pivot • G (or L) holds all elements & other partition empty • Complexity becomes equal to Insertion-Sort

  37. Simple Pivot Selection • Could use first (or last) element as pivot • It’s easy to code… • …but also makes sorted data the worst case… • …which is common so this is a really bad idea • Could choose random element as pivot • Little harder to code, but expected to work well • Best is median of first, mid, & last items in list • Nearly eliminates worst case, but much harder to code

  38. For Next Lecture • Week #9 assignment posted so get working • Keep reviewing requirements for program #2 • 2nd preliminary deadline coming up so get started • Time developing good tests saves coding time later • Reading on radix sort for Monday • Is it possible to sort without comparisons? • How is radix sort faster than O(n log n) lower bound?

More Related