This preview shows pages 1–7. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Sorting in Linear Time Data Structures and Algorithms Andrei Bulatov Algorithms Sorting in Linear Time 72 Comparison Sorts The only test that all the algorithms we have considered so far is comparison The only information we obtain about an input sequence is given by or We will assume that all the input numbers are different. Then the it suffices to use only one comparison n a a a , , , 2 1 K j i j i j i j i a a a a a a a a = < , , , j i a a j i a a Algorithms Sorting in Linear Time 73 The Decision Tree Model Observe that for different input permutations, even if they differ only in the order of elements, our algorithm must perform different actions Therefore, we view its work as recognizing a permutation It is convenient to represent this process as a decision tree 1:2 2:3 1:3 1:3 2:3 1,2,3 1,2,3 1,3,2 1,3,2 3,1,2 3,1,2 2,3,1 2,3,1 3,2,1 3,2,1 2,1,3 2,1,3 Algorithms Sorting in Linear Time 74 Decision Trees Let the input sequence contain n elements We assume they are numbers from 1 to n Therefore, the input sequence is a permutation (1), (2), , (n) Each internal node is labeled by i : j for some i and j in the range 1 i,j n Execution of the algorithm is a path from the root to a leaf At each internal node a comparison is made Depending on the outcome either comparisons on the left or the right subtree When we come to a leaf , the algorithm recognizes a particular ordering j i a a Algorithms Sorting in Linear Time 75 Lower Bound Proof There are n! permutations of n elements. The algorithm should recognize all of them Therefore the decision tree contains k n! leaves The complete binary tree has leaves where h is the height of the tree, the length of the longest roottoleaf path Finally, note that the height of the tree is running time in the worst case Theorem Any comparison sort algorithm requires (mn) comparisons in the worst case h 2 h k n 2 ! ) log ( ) ! log( n n n h = Algorithms Sorting in Linear Time 76 Counting Sort Suppose we are allowed to do more than just comparisons...
View Full
Document
 Fall '09
 A.BULATOV
 Algorithms, Data Structures, Sort

Click to edit the document details