1 / 24

Tutorial 9 Sort

Tutorial 9 Sort. Divide & Conquer. Solves a big problem by dividing the big problem into small ones and solve the small problems, and combine the solutions of small problems to get the solution of the original big problem Example QuickSort MergeSort. QuickSort. Divide Step

zuri
Download Presentation

Tutorial 9 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. Tutorial 9Sort

  2. Divide & Conquer • Solves a big problem by dividing the big problem into small ones and solve the small problems, and combine the solutions of small problems to get the solution of the original big problem • Example • QuickSort • MergeSort

  3. QuickSort • Divide Step • Pick any number v as pivot • Divide set S into two subsets S1 and S2 • , • . • Conquer Step • Sort S1 and S2 • Combine Step • Sort S by the sorted S1 followed by v, and followed by the sorted S2

  4. 81 31 57 75 43 13 0 92 65 26 31 57 75 81 43 13 65 26 0 92 31 57 13 26 0 43 81 92 75 75 81 92 0 13 26 31 43 57 0 13 26 31 43 57 65 75 81 92 A Quicksort Example Select 65 a pivot Divide by partition 65 Do the same to select a pivot, … Do the same to select a pivot, … Combine Sorting

  5. Merge Sort • Divide Step • Divide the set S into two subsets S1 and S2 • S1 contains the first elements • S2 contains the remaining elements • Conquer Step • Sort S1 and S2 recursively • Combine Step • Merge the sorted S1 and S2

  6. 7 2 9 4  2 4 7 9 3 8 6 1  1 3 8 6 7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 An Example (1) • Partition the initial sequence into two subsequences. 7 2 9 4  3 8 6 11 2 3 4 6 7 8 9 Merge Sort

  7. 7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 An Example (2) • Recursive call, partition the first sequence into another two subsequences 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 8 6 Merge Sort

  8. 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 An Example (3) • Recursive call, partition 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 8 6 7  2 2 7 9 4  4 9 3 8  3 8 6 1  1 6 Merge Sort

  9. 7  2 2 7 9 4  4 9 3 8  3 8 6 1  1 6 An Example (4) • Recursive call, 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 3 8 6 1  1 3 8 6 77 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

  10. An Example (5) • Recursive call, 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 3 8 6 1  1 3 8 6 7  2 2 7 9 4  4 9 3 8  3 8 6 1  1 6 77 22 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

  11. An Example (6) • Merge two sequences into a longer sorted 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 3 8 6 1  1 3 8 6 7  22 7 9 4  4 9 3 8  3 8 6 1  1 6 77 22 9  9 4  4 3  3 8  8 6  6 1  1 Merge Sort

  12. An Example (7) • Recursive call, …, base case, 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 3 8 6 1  1 3 8 6 7  22 7 9 4  4 9 3 8  3 8 6 1  1 6 77 22 9 9 4 4 3  3 8  8 6  6 1  1 Merge Sort

  13. An Example (8) • Merge two sequences into a longer sorted 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 3 8 6 1  1 3 8 6 7  22 7 9 4  4 9 3 8  3 8 6 1  1 6 77 22 9 9 4 4 3  3 8  8 6  6 1  1 Merge Sort

  14. An Example (9) • Recursive call, …, merge, 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 3 8 6 1  1 3 6 8 7  22 7 9 4  4 9 3 8 3 8 6 1  1 6 77 22 9 9 4 4 33 88 66 11 Merge Sort

  15. An Example (10) • Merge two sequences into a longer sorted 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 3 8 6 1  1 3 6 8 7  22 7 9 4  4 9 3 8 3 8 6 1  1 6 77 22 9 9 4 4 33 88 66 11 Merge Sort

  16. Quick Sort & Merge Sort • Time complexity O(nlogn)

  17. Exercise Sort 10 records with keys (26, 5, 37, 1, 61, 11, 59, 15, 48, 19)

  18. Solution

  19. Exercises • Show that QuickSort takes O(n^2) time when the input list is already in sorted order

  20. Solution • F (n) =O (n) + F (n-1)  F (n) =O(n^2)

  21. Exercise • Quick sort is an unstable sorting method. Give an example of an input list in which the order of records with equal keys is not preserved.

  22. Solution

  23. Exercise • Prove the MergeSort is stable

  24. Solution

More Related