1 / 16

Sorting and Master Method Neil Tang 01/21/2009

Sorting and Master Method Neil Tang 01/21/2009. Class Overview. Review of sorting algorithms: Insertion, merge and quick The master method. Insertion Sort. Start from the second element and select an element (key) in each step.

catron
Download Presentation

Sorting and Master Method Neil Tang 01/21/2009

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. Sorting and Master Method Neil Tang01/21/2009 CS223 Advanced Data Structures and Algorithms

  2. Class Overview • Review of sorting algorithms: Insertion, merge and quick • The master method CS223 Advanced Data Structures and Algorithms

  3. Insertion Sort • Start from the second element and select an element (key) in each step. • Insert the key to the current sorted subsequence such that the new subsequence is in the sorted order. CS223 Advanced Data Structures and Algorithms

  4. key key 8 5 2 3 5 8 3 3 5 2 8 3 i j i j key 2 key 3 3 5 2 8 3 2 3 5 8 3 i j i j 2 3 5 8 3 2 3 3 5 8 Insertion Sort CS223 Advanced Data Structures and Algorithms

  5. Insertion Sort Insertion-Sort(A[1..n]) 1 for j := 2 to n do 2 key := A[j] i := j -1 5 while i > 0 and A[i] > key do 6 A[i+1] := A[i] 7 i := i - 1 8 A[i+1] := key CS223 Advanced Data Structures and Algorithms

  6. Insertion Sort • Best case: the array is sorted • each while loop only takes constant time, which is repeated (n-1) times, so time complexity is Θ(n) . • Worst case: the array is in reverse order • each while loop takes cj time. So time complexity is CS223 Advanced Data Structures and Algorithms

  7. Merge Sort • Divide:Divide the N-element sequence into 2 subsequences of N/2 each. • Conquer:Sort each subsequence recursively using merge sort. • Combine:Merge two sorted subsequences to produce a single sorted sequence. • The time complexity is O(NlogN) CS223 Advanced Data Structures and Algorithms

  8. Merge Sort CS223 Advanced Data Structures and Algorithms

  9. Quick Sort • Divide:Divide the sequence into 2 subsequences, s.t. each element in the 1st subsequence is less than or equal to each element in the 2nd subsequence. • Conquer:Sort each subsequence recursively using quick sort. • Combine:no work is needed. CS223 Advanced Data Structures and Algorithms

  10. age 25 people age  25 People age < 25 people age < 23 age 23 people age  23 people age < 30 age 30 people age  30 Quick Sort CS223 Advanced Data Structures and Algorithms

  11. Master Method • Recurrence in general form: T(N) = aT(N/b) + f(N), a  1 and b > 1. • Case 1: If f(N) = O(Nlogba - ) for some constant  > 0, then T(N) = (Nlogba) . • Case 2: If f(N) = (Nlogba), then T(N) = (Nlogba logN). • Case 3: If f(N) = (Nlogba + ) for some constant  > 0, and a*f(N/b)c*f(N) for some constant c < 1 and all sufficiently large N, then T(N) = (f(N)) . CS223 Advanced Data Structures and Algorithms

  12. Example: Binary Search • T(N) = T(N/2) + 1 • a=1, b=2, f(N)=1 • log21 = 0 • So we have case 2, T(N) = (logN). CS223 Advanced Data Structures and Algorithms

  13. Example: Merge Sort • T(N) = 2T(N/2) + N • a=2, b=2, f(N)=N • log22 = 1 • So we have case 2, T(N) = (NlogN). CS223 Advanced Data Structures and Algorithms

  14. Example: Matrix Multiplication • T(N) = 8T(N/2) + N2 • a=8, b=2, f(N)=N2 • log28 = 3  •  = 0.5 • So we have case 1, T(N) = (N3). CS223 Advanced Data Structures and Algorithms

  15. More Example • T(N) = 3T(N/4) + NlogN • a=3, b=4, f(N) = NlogN • log43 = 0.7925  • =0.2, c=3/4 • So we have case 3, T(N) = (NlogN). CS223 Advanced Data Structures and Algorithms

  16. Limitations • Does the master method work all the time? • Quick sort: T(N) = T(N-1) + N • Fibonacci number: T(N) = T(N-1) + T(N-2) + 1 • Does it work for all the cases in the format of T(N) =aT(N/b) + f(N)? CS223 Advanced Data Structures and Algorithms

More Related