07 - Sorting in Linear Time Data Structures and Algorithms...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Sorting in Linear Time Data Structures and Algorithms Andrei Bulatov Algorithms Sorting in Linear Time 7-2 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 7-3 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 7-4 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 7-5 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 root-to-leaf 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 7-6 Counting Sort Suppose we are allowed to do more than just comparisons...
View Full Document

Page1 / 22

07 - Sorting in Linear Time Data Structures and Algorithms...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online