Sorting and Master Method Neil Tang 01212009 CS

  • Slides: 16
Download presentation
Sorting and Master Method Neil Tang 01/21/2009 CS 223 Advanced Data Structures and Algorithms

Sorting and Master Method Neil Tang 01/21/2009 CS 223 Advanced Data Structures and Algorithms 1

Class Overview Ø Review of sorting algorithms: Insertion, merge and quick Ø The master

Class Overview Ø Review of sorting algorithms: Insertion, merge and quick Ø The master method CS 223 Advanced Data Structures and Algorithms 2

Insertion Sort Ø Start from the second element and select an element (key) in

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. CS 223 Advanced Data Structures and Algorithms 3

Insertion Sort 5 key 3 5 2 8 3 i j 8 key 2

Insertion Sort 5 key 3 5 2 8 3 i j 8 key 2 3 5 8 3 i j 2 key 3 5 2 8 3 i j 2 3 5 8 3 CS 223 Advanced Data Structures and Algorithms 3 key 2 3 5 8 3 i j 2 3 3 5 8 4

Insertion Sort Insertion-Sort(A[1. . n]) 1 for j : = 2 to n do

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 CS 223 Advanced Data Structures and Algorithms 5

Insertion Sort Ø Best case: the array is sorted each while loop only takes

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 CS 223 Advanced Data Structures and Algorithms 6

Merge Sort Ø Divide: Divide the N-element sequence into 2 subsequences of N/2 each.

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(Nlog. N) CS 223 Advanced Data Structures and Algorithms 7

Merge Sort CS 223 Advanced Data Structures and Algorithms 8

Merge Sort CS 223 Advanced Data Structures and Algorithms 8

Quick Sort Ø Divide: Divide the sequence into 2 subsequences, s. t. each element

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

Quick Sort People age < 25 people age < 23 age people 23 age

Quick Sort People age < 25 people age < 23 age people 23 age 25 people age < 30 30 CS 223 Advanced Data Structures and Algorithms people age 30 10

Master Method Ø Recurrence in general form: T(N) = a. T(N/b) + f(N), a

Master Method Ø Recurrence in general form: T(N) = a. T(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 log. N). Ø 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)). CS 223 Advanced Data Structures and Algorithms 11

Example: Binary Search Ø T(N) = T(N/2) + 1 Ø a=1, b=2, f(N)=1 Ø

Example: Binary Search Ø T(N) = T(N/2) + 1 Ø a=1, b=2, f(N)=1 Ø log 21 = 0 Ø So we have case 2, T(N) = (log. N). CS 223 Advanced Data Structures and Algorithms 12

Example: Merge Sort Ø T(N) = 2 T(N/2) + N Ø a=2, b=2, f(N)=N

Example: Merge Sort Ø T(N) = 2 T(N/2) + N Ø a=2, b=2, f(N)=N Ø log 22 = 1 Ø So we have case 2, T(N) = (Nlog. N). CS 223 Advanced Data Structures and Algorithms 13

Example: Matrix Multiplication Ø T(N) = 8 T(N/2) + N 2 Ø a=8, b=2,

Example: Matrix Multiplication Ø T(N) = 8 T(N/2) + N 2 Ø a=8, b=2, f(N)=N 2 Ø log 28 = 3 Ø = 0. 5 Ø So we have case 1, T(N) = (N 3). CS 223 Advanced Data Structures and Algorithms 14

More Example Ø T(N) = 3 T(N/4) + Nlog. N Ø a=3, b=4, f(N)

More Example Ø T(N) = 3 T(N/4) + Nlog. N Ø a=3, b=4, f(N) = Nlog. N Ø log 43 = 0. 7925 Ø =0. 2, c=3/4 Ø So we have case 3, T(N) = (Nlog. N). CS 223 Advanced Data Structures and Algorithms 15

Limitations Ø Does the master method work all the time? Ø Quick sort: T(N)

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) =a. T(N/b) + f(N)? CS 223 Advanced Data Structures and Algorithms 16