{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

chap7-solutions - ˛ One iteration reduces the number of...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Selected Solutions for Chapter 7: Quicksort Solution to Exercise 7.2-3 P ARTITION does a “worst-case partitioning” when the elements are in decreasing order. It reduces the size of the subarray under consideration by only 1 at each step, which we’ve seen has running time ‚.n 2 / . In particular, P ARTITION , given a subarray AŒp : : r of distinct elements in de- creasing order, produces an empty partition in AŒp : : q NUL 1 , puts the pivot (orig- inally in AŒr ) into AŒp , and produces a partition AŒp C 1 : : r with only one fewer element than AŒp : : r . The recurrence for Q UICKSORT becomes T .n/ D T .n NUL 1/ C ‚.n/ , which has the solution T .n/ D ‚.n 2 / . Solution to Exercise 7.2-5 The minimum depth follows a path that always takes the smaller part of the parti- tion—i.e., that multiplies the number of elements by
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ˛ . One iteration reduces the number of elements from n to ˛n , and i iterations reduces the number of elements to ˛ i n . At a leaf, there is just one remaining element, and so at a minimum-depth leaf of depth m , we have ˛ m n D 1 . Thus, ˛ m D 1=n . Taking logs, we get m lg ˛ D N lg n , or m D N lg n= lg ˛ . Similarly, maximum depth corresponds to always taking the larger part of the par-tition, i.e., keeping a fraction 1 N ˛ of the elements each time. The maximum depth M is reached when there is one element left, that is, when .1 N ˛/ M n D 1 . Thus, M D N lg n= lg .1 N ˛/ . All these equations are approximate because we are ignoring floors and ceilings....
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online