09.12.2012 Views

Concrete mathematics : a foundation for computer science

Concrete mathematics : a foundation for computer science

Concrete mathematics : a foundation for computer science

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

28 SUMS<br />

Let’s apply these ideas to a recurrence that arises in the study of “quicksort,”<br />

one of the most important methods <strong>for</strong> sorting data inside a <strong>computer</strong>. (Quicksort was<br />

The average number of comparison steps made by quicksort when it is applied<br />

to n items in random order satisfies the recurrence<br />

invented bY H0arc<br />

in 1962 [158].)<br />

k=O<br />

<strong>for</strong> n > 0.<br />

(2.12)<br />

Hmmm. This looks much scarier than the recurrences we’ve seen be<strong>for</strong>e; it<br />

includes a sum over all previous values, and a division by n. Trying small<br />

cases gives us some data (Cl = 2, Cl = 5, CX = T) but doesn’t do anything<br />

to quell our fears.<br />

We can, however, reduce the complexity of (2.12) systematically, by first<br />

getting rid of the division and then getting rid of the 1 sign. The idea is to<br />

multiply both sides by n, obtaining the relation<br />

n-1<br />

nC, =n2+n+2xCk, <strong>for</strong> n > 0;<br />

k=O<br />

hence, if we replace n by n - 1,<br />

n-2<br />

(n-l)cnpj = (n-1)2+(n-1)+2xck, <strong>for</strong>n-1 >O.<br />

We can now subtract the second equation from the first, and the 1 sign<br />

disappears:<br />

k=O<br />

nC, - (n - 1)&l = 2n + 2C,-1 , <strong>for</strong> n > 1.<br />

It turns out that this relation also holds when n = 1, because Cl = 2. There<strong>for</strong>e<br />

the original recurrence <strong>for</strong> C, reduces to a much simpler one:<br />

co = 0;<br />

nC, = (n + 1 )C,-I + 2n, <strong>for</strong> n > 0.<br />

Progress. We’re now in a position to apply a summation factor, since this<br />

recurrence has the <strong>for</strong>m of (2.9) with a, = n, b, = n + 1, and c, = 2n.<br />

The general method described on the preceding page tells us to multiply the<br />

recurrence through by some multiple of<br />

a,._1 an-l. . . a1 (n-l).(n-2).....1 2<br />

S n = b,b,-, . . b2 = (n+l).n...:3 = (n+l)n

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!