CMPS 2200 – Fall 2017 Sorting Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk 11/17/17 1 CMPS 2200 Intro. to Algorithms
How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts : only use comparisons to determine the relative order of elements. • E.g ., insertion sort, merge sort, heapsort. The best worst-case running time that we’ve seen for comparison sorting is O ( n log n ). Is O(nlogn) the best we can do? Decision trees can help us answer this question. 11/17/17 2 CMPS 2200 Intro. to Algorithms
Decision-tree model A decision tree models the execution of any comparison sorting algorithm: • One tree per input size n . • The tree contains all possible comparisons (= if-branches) that could be executed for any input of size n . • The tree contains all comparisons along all possible instruction traces (= control flows) for all inputs of size n . • For one input, only one path to a leaf is executed. • Running time = length of the path taken. • Worst-case running time = height of tree. 11/17/17 3 CMPS 2200 Intro. to Algorithms
Decision-tree for insertion sort: Sort a 1 , a 2 , a 3 a 1 a 2 a 3 insert a 2 a 1 : a 2 j i insert a 3 insert a 3 a 2 a 1 a 3 a 2 : a 3 a 1 : a 3 a 1 a 2 a 3 j i j i a 1 a 2 a 3 a 2 a 1 a 3 j i a 1 a 2 a 3 a 1 : a 3 a 2 a 1 a 3 a 2 : a 3 i j a 1 a 3 a 2 a 3 a 1 a 2 a 2 a 3 a 1 a 3 a 2 a 1 Each internal node is labeled a i : a j for i , j {1, 2,…, n }. • The left subtree shows subsequent comparisons if a i a j . • The right subtree shows subsequent comparisons if a i a j . 11/17/17 4 CMPS 2200 Intro. to Algorithms
Decision-tree for insertion sort Sort a 1 , a 2 , a 3 insert a 2 a 1 a 2 a 3 j i a 1 : a 2 insert a 3 insert a 3 a 2 a 1 a 3 a 2 : a 3 a 1 : a 3 a 1 a 2 a 3 j i j i a 1 a 2 a 3 a 2 a 1 a 3 j i a 1 a 2 a 3 a 1 : a 3 a 2 a 1 a 3 a 2 : a 3 i j a 1 a 3 a 2 a 3 a 1 a 2 a 2 a 3 a 1 a 3 a 2 a 1 Each internal node is labeled a i : a j for i , j {1, 2,…, n }. • The left subtree shows subsequent comparisons if a i a j . • The right subtree shows subsequent comparisons if a i a j . 11/17/17 5 CMPS 2200 Intro. to Algorithms
Decision-tree for insertion sort Sort a 1 , a 2 , a 3 insert a 2 a 1 a 2 a 3 j i a 1 : a 2 9 4 insert a 3 insert a 3 a 2 a 1 a 3 a 2 : a 3 a 1 : a 3 a 1 a 2 a 3 j i j i a 1 a 2 a 3 a 2 a 1 a 3 j i a 1 a 2 a 3 a 1 : a 3 a 2 a 1 a 3 a 2 : a 3 i j a 1 a 3 a 2 a 3 a 1 a 2 a 2 a 3 a 1 a 3 a 2 a 1 Each internal node is labeled a i : a j for i , j {1, 2,…, n }. • The left subtree shows subsequent comparisons if a i a j . • The right subtree shows subsequent comparisons if a i a j . 11/17/17 6 CMPS 2200 Intro. to Algorithms
Decision-tree for insertion sort Sort a 1 , a 2 , a 3 insert a 2 a 1 a 2 a 3 j i a 1 : a 2 insert a 3 insert a 3 a 2 a 1 a 3 a 2 : a 3 a 1 : a 3 a 1 a 2 a 3 j i 9 6 j i a 1 a 2 a 3 a 2 a 1 a 3 j i a 1 a 2 a 3 a 1 : a 3 a 2 a 1 a 3 a 2 : a 3 i j a 1 a 3 a 2 a 3 a 1 a 2 a 2 a 3 a 1 a 3 a 2 a 1 Each internal node is labeled a i : a j for i , j {1, 2,…, n }. • The left subtree shows subsequent comparisons if a i a j . • The right subtree shows subsequent comparisons if a i a j . 11/17/17 7 CMPS 2200 Intro. to Algorithms
Decision-tree for insertion sort Sort a 1 , a 2 , a 3 insert a 2 a 1 a 2 a 3 j i a 1 : a 2 insert a 3 insert a 3 a 2 a 1 a 3 a 2 : a 3 a 1 : a 3 a 1 a 2 a 3 j i j i a 1 a 2 a 3 a 2 a 1 a 3 j i a 1 a 2 a 3 a 1 : a 3 a 2 a 1 a 3 a 2 : a 3 i j 4 6 a 1 a 3 a 2 a 3 a 1 a 2 a 2 a 3 a 1 a 3 a 2 a 1 Each internal node is labeled a i : a j for i , j {1, 2,…, n }. • The left subtree shows subsequent comparisons if a i a j . • The right subtree shows subsequent comparisons if a i a j . 11/17/17 8 CMPS 2200 Intro. to Algorithms
Decision-tree for insertion sort Sort a 1 , a 2 , a 3 insert a 2 a 1 a 2 a 3 j i a 1 : a 2 insert a 3 insert a 3 a 2 a 1 a 3 a 2 : a 3 a 1 : a 3 a 1 a 2 a 3 j i j i a 1 a 2 a 3 a 2 a 1 a 3 j i a 1 a 2 a 3 a 1 : a 3 a 2 a 1 a 3 a 2 : a 3 i j a 1 a 3 a 2 a 3 a 1 a 2 a 2 a 3 a 1 a 3 a 2 a 1 4 6 9 Each internal node is labeled a i : a j for i , j {1, 2,…, n }. • The left subtree shows subsequent comparisons if a i a j . • The right subtree shows subsequent comparisons if a i a j . 11/17/17 9 CMPS 2200 Intro. to Algorithms
Decision-tree for insertion sort Sort a 1 , a 2 , a 3 insert a 2 a 1 a 2 a 3 j i a 1 : a 2 insert a 3 insert a 3 a 2 a 1 a 3 a 2 : a 3 a 1 : a 3 a 1 a 2 a 3 j i j i a 1 a 2 a 3 a 2 a 1 a 3 j i a 1 a 2 a 3 a 1 : a 3 a 2 a 1 a 3 a 2 : a 3 i j a 1 a 3 a 2 a 3 a 1 a 2 a 2 a 3 a 1 a 3 a 2 a 1 4 6 9 Each leaf contains a permutation , ,…, ( n ) to indicate that the ordering a (1) a (2) a (n) has been established. 11/17/17 10 CMPS 2200 Intro. to Algorithms
Decision-tree model A decision tree models the execution of any comparison sorting algorithm: • One tree per input size n . • The tree contains all possible comparisons (= if-branches) that could be executed for any input of size n . • The tree contains all comparisons along all possible instruction traces (= control flows) for all inputs of size n . • For one input, only one path to a leaf is executed. • Running time = length of the path taken. • Worst-case running time = height of tree. 11/17/17 11 CMPS 2200 Intro. to Algorithms
Lower bound for comparison sorting Theorem. Any decision tree that can sort n elements must have height ( n log n ). Proof. The tree must contain n ! leaves, since there are n ! possible permutations. For a binary tree of height- h holds that #leaves 2 h . Thus, n ! 2 h . h log( n !) (log is mono. increasing) log (( n /2) n/ 2 ) = n/ 2 log n/ 2 h ( n log n ) . 11/17/17 12 CMPS 2200 Intro. to Algorithms
Lower bound for comparison sorting Corollary. Mergesort is an asymptotically optimal comparison sorting algorithm. 11/17/17 13 CMPS 2200 Intro. to Algorithms
Sorting in linear time Counting sort: No comparisons between elements. • Input : A [0 . . n- 1], where A [ j ] {0, 1, 2, …, k- 1} . • Output : B [0 . . n- 1], sorted. • Auxiliary storage : C [0 . . k- 1] . 11/17/17 14 CMPS 2200 Intro. to Algorithms
Counting sort for ( i = 0; i < k ; i++ ) 1. C [ i ] = 0 for ( j = 0; i < n ; j++ ) 2. C [ A [ j ]] = C [ A [ j ]] + 1 // C [ i ] == |{key = i }| for ( i = 1; i < k ; i++ ) 3. // C [ i ] == |{key i }| C [ i ] = C [ i ] + C [ i –1] for ( j = n- 1; i ≥ 0; j-- ) 4. B [ C [ A [ j ]]-1] = A[ j ] C [ A [ j ]] = C [ A [ j ]] – 1 11/17/17 15 CMPS 2200 Intro. to Algorithms
Counting-sort example 0 1 2 3 4 0 1 2 3 A : 3 0 2 3 2 C : B : 11/17/17 16 CMPS 2200 Intro. to Algorithms
Loop 1 0 1 2 3 4 0 1 2 3 A : 3 0 2 3 2 C : 0 0 0 0 B : for ( i = 0; i < k ; i++ ) 1. C [ i ] = 0 11/17/17 17 CMPS 2200 Intro. to Algorithms
Loop 2 0 1 2 3 4 0 1 2 3 A : 3 0 2 3 2 C : 0 0 0 1 B : for ( j = 0; i < n ; j++ ) 2. C [ A [ j ]] = C [ A [ j ]] + 1 // C [ i ] == |{key = i }| 11/17/17 18 CMPS 2200 Intro. to Algorithms
Loop 2 0 1 2 3 4 0 1 2 3 A : 3 0 2 3 2 C : 1 0 0 1 B : for ( j = 0; i < n ; j++ ) 2. C [ A [ j ]] = C [ A [ j ]] + 1 // C [ i ] == |{key = i }| 11/17/17 19 CMPS 2200 Intro. to Algorithms
Loop 2 0 1 2 3 4 0 1 2 3 A : 3 0 2 3 2 C : 1 0 1 1 B : for ( j = 0; i < n ; j++ ) 2. C [ A [ j ]] = C [ A [ j ]] + 1 // C [ i ] == |{key = i }| 11/17/17 20 CMPS 2200 Intro. to Algorithms
Loop 2 0 1 2 3 4 0 1 2 3 A : 3 0 2 3 2 C : 1 0 1 2 B : for ( j = 0; i < n ; j++ ) 2. C [ A [ j ]] = C [ A [ j ]] + 1 // C [ i ] == |{key = i }| 11/17/17 21 CMPS 2200 Intro. to Algorithms
Loop 2 0 1 2 3 4 0 1 2 3 A : 3 0 2 3 2 C : 1 0 2 2 B : for ( j = 0; i < n ; j++ ) 2. C [ A [ j ]] = C [ A [ j ]] + 1 // C [ i ] == |{key = i }| 11/17/17 22 CMPS 2200 Intro. to Algorithms
Loop 3 0 1 2 3 4 0 1 2 3 A : 3 0 2 3 2 C : 1 0 2 2 B : C' : 1 1 2 2 for ( i = 1; i < k ; i++ ) 3. // C [ i ] == |{key i }| C [ i ] = C [ i ] + C [ i –1] 11/17/17 23 CMPS 2200 Intro. to Algorithms
Loop 3 0 1 2 3 4 0 1 2 3 A : 3 0 2 3 2 C : 1 0 2 2 B : C' : 1 1 3 2 for ( i = 1; i < k ; i++ ) 3. // C [ i ] == |{key i }| C [ i ] = C [ i ] + C [ i –1] 11/17/17 24 CMPS 2200 Intro. to Algorithms
Loop 3 0 1 2 3 4 0 1 2 3 A : 3 0 2 3 2 C : 1 0 2 2 B : C' : 1 1 3 5 for ( i = 1; i < k ; i++ ) 3. // C [ i ] == |{key i }| C [ i ] = C [ i ] + C [ i –1] 11/17/17 25 CMPS 2200 Intro. to Algorithms
Recommend
More recommend