lect17-linear-time-sort

# lect17-linear-time-sort - Lecture Notes CMSC 251 Using...

This preview shows pages 1–2. Sign up to view the full content.

Lecture Notes CMSC 251 Using Decision Trees for Analyzing Sorting: Consider any sorting algorithm. Let T ( n ) be the maximum number of comparisons that this algorithm makes on any input of size n . Notice that the running time fo the algorithm must be at least as large as T ( n ) , since we are not counting data movement or other computations at all. The algorithm deﬁnes a decision tree. Observe that the height of the decision tree is exactly equal to T ( n ) , because any path from the root to a leaf corresponds to a sequence of comparisons made by the algorithm. As we have seen earlier, any binary tree of height T ( n ) has at most 2 T ( n ) leaves. This means that this sorting algorithm can distinguish between at most 2 T ( n ) different ﬁnal actions. Let’s call this quantity A ( n ) , for the number of different ﬁnal actions the algorithm can take. Each action can be thought of as a speciﬁc way of permuting the oringinal input to get the sorted output. How many possible actions must any sorting algorithm distinguish between? If the input consists of n distinct numbers, then those numbers could be presented in any of n ! different permutations. For each different permutation, the algorithm must “unscramble” the numbers in an essentially different way, that is it must take a different action, implying that A ( n ) n ! . (Again, A ( n ) is usually not exactly equal to n ! because most algorithms contain some redundant unreachable leaves.) Since A ( n ) 2 T ( n ) we have 2 T ( n ) n ! , implying that T ( n ) lg( n !) . We can use Stirling’s approximation for n ! (see page 35 in CLR) yielding: n ! 2 πn ± n e ² n T ( n ) lg ± 2 ± n e ² n ² =l g 2 + n lg n - n lg e Ω( n log n ) . Thus we have, the following theorem. Theorem: Any comparison-based sorting algorithm has worst-case running time Ω( n log n ) . This can be generalized to show that the average-case time to sort is also Ω( n log n ) (by arguing about the average height of a leaf in a tree with at least n ! leaves). The lower bound on sorting can be generalized to provide lower bounds to a number of other problems as well. Lecture 17: Linear Time Sorting (Tuesday, Mar 31, 1998) Read: Chapt. 9 of CLR. Linear Time Sorting: Last time we presented a proof that it is not possible to sort faster than Ω( n log n ) time assuming that the algorithm is based on making 2-way comparisons. Recall that the argument was based on showing that any comparison-based sorting could be represented as a decision tree, the decision tree must have at least n ! leaves, to distinguish between the n ! different permutations in which the keys could be input, and hence its height must be at least lg( n !) Ω( n lg n ) .

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/13/2012 for the course CMSC 351 taught by Professor Staff during the Fall '11 term at University of Louisville.

### Page1 / 4

lect17-linear-time-sort - Lecture Notes CMSC 251 Using...

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

View Full Document
Ask a homework question - tutors are online