240 likes | 368 Vues
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
E N D
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 • 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
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
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
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 11 2 3 4 6 7 8 9 Merge Sort
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 11 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
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 11 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
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 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 77 2 2 9 9 4 4 3 3 8 8 6 6 1 1 Merge Sort
An Example (5) • Recursive call, base case 7 2 9 4 3 8 6 11 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 77 22 9 9 4 4 3 3 8 8 6 6 1 1 Merge Sort
An Example (6) • Merge two sequences into a longer sorted sequence. 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 3 3 8 8 6 6 1 1 Merge Sort
An Example (7) • Recursive call, …, base case, merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 3 3 8 8 6 6 1 1 Merge Sort
An Example (8) • Merge two sequences into a longer sorted sequence. 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 42 4 7 9 3 8 6 1 1 3 8 6 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 3 3 8 8 6 6 1 1 Merge Sort
An Example (9) • Recursive call, …, merge, merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 42 4 7 9 3 8 6 1 1 3 6 8 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 33 88 66 11 Merge Sort
An Example (10) • Merge two sequences into a longer sorted sequence. 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 42 4 7 9 3 8 6 1 1 3 6 8 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 33 88 66 11 Merge Sort
Quick Sort & Merge Sort • Time complexity O(nlogn)
Exercise Sort 10 records with keys (26, 5, 37, 1, 61, 11, 59, 15, 48, 19)
Exercises • Show that QuickSort takes O(n^2) time when the input list is already in sorted order
Solution • F (n) =O (n) + F (n-1) F (n) =O(n^2)
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.
Exercise • Prove the MergeSort is stable