cs223 advanced data structures and algorithms 1 sorting and master method neil tang 01/21/2009
TRANSCRIPT
CS223 Advanced Data Structures and Algorithms 1
Sorting and Master Method Sorting and Master Method
Neil TangNeil Tang01/21/200901/21/2009
CS223 Advanced Data Structures and Algorithms 2
Class OverviewClass Overview
Review of sorting algorithms: Insertion, merge and quick
The master method
CS223 Advanced Data Structures and Algorithms 3
Insertion SortInsertion 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
Insertion SortInsertion Sort
3 5 2 8
5
ji
key
3
3 5 2 8
2
ji
key
3
2 3 5 8 3
2 3 5 8
8
ji
key
3
2 3 5 8
3
ji
key
3
2 3 3 5 8
CS223 Advanced Data Structures and Algorithms
Insertion SortInsertion Sort
Insertion-Sort(A[1..n])1 for j := 2 to n do 2 key := A[j]
i := j -15 while i > 0 and A[i] > key do 6 A[i+1] := A[i]7 i := i - 18 A[i+1] := key
5
Insertion SortInsertion Sort
CS223 Advanced Data Structures and Algorithms 6
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
)(2
)1)(2( 2
2
nnnc
jcn
j
CS223 Advanced Data Structures and Algorithms 7
Merge SortMerge 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 SortMerge Sort
5, 3, 6, 8, 12, 3, 1, 4, 7, 9, 13, 5, 6, 8, 12 1, 3, 4, 7, 9, 14
1, 3, 3, 4, 5, 6, 7, 8, 9, 12, 14
CS223 Advanced Data Structures and Algorithms 9
Quick SortQuick 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
Quick SortQuick Sort
age25
Peopleage < 25
peopleage 25
age23
peopleage < 23
age30
peopleage 30
peopleage 23
peopleage < 30
CS223 Advanced Data Structures and Algorithms 11
Master MethodMaster Method
Recurrence in general form: T(N) = aT(N/b) + f(N), a 1 and b > 1.
Case 1: If f(N) = O(Nlogb
a - ) for some constant > 0, then T(N) = (Nlog
ba) .
Case 2: If f(N) = (Nlogb
a), then T(N) = (Nlogb
a logN).
Case 3: If f(N) = (Nlogb
a + ) 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 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 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 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 ExampleMore 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
LimitationsLimitations
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)?