160 likes | 167 Views
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.
E N D
Sorting and Master Method Neil Tang01/21/2009 CS223 Advanced Data Structures and Algorithms
Class Overview • Review of sorting algorithms: Insertion, merge and quick • The master method CS223 Advanced Data Structures and Algorithms
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
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
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
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
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
Merge Sort CS223 Advanced Data Structures and Algorithms
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
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
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
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
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
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
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
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