17.01.2013 Views

Data Structures and Algorithm Analysis in C - SVS

Data Structures and Algorithm Analysis in C - SVS

Data Structures and Algorithm Analysis in C - SVS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Structures</strong>, <strong>Algorithm</strong> <strong>Analysis</strong>: CHAPTER 7: SORTING<br />

Figure 7.15 A small change to quicksort, which breaks the algorithm<br />

Worst-Case <strong>Analysis</strong><br />

Best-Case <strong>Analysis</strong><br />

Average-Case <strong>Analysis</strong><br />

Worst-Case <strong>Analysis</strong><br />

The pivot is the smallest element, all the time. Then i = 0 <strong>and</strong> if we ignore T(0)<br />

= 1, which is <strong>in</strong>significant, the recurrence is<br />

T(n) = T(n - 1) + cn, n > 1<br />

(7.2)<br />

We telescope, us<strong>in</strong>g Equation (7.2) repeatedly. Thus<br />

T(n -1) = T(n - 2) + c(n - 1)<br />

(7.3)<br />

T(n - 2) = T(n - 3) + c(n - 2)<br />

(7.4)<br />

...<br />

T(2) = T(1) + c(2)<br />

(7.5)<br />

Add<strong>in</strong>g up all these equations yields<br />

(7.6)<br />

as claimed earlier.<br />

Best-Case <strong>Analysis</strong><br />

In the best case, the pivot is <strong>in</strong> the middle. To simplify the math, we assume<br />

that the two subfiles are each exactly half the size of the orig<strong>in</strong>al, <strong>and</strong><br />

although this gives a slight overestimate, this is acceptable because we are only<br />

<strong>in</strong>terested <strong>in</strong> a Big-Oh answer.<br />

T(n) = 2T(n/2) + cn<br />

mk:@MSITStore:K:\<strong>Data</strong>.<strong>Structures</strong>.<strong>and</strong>.<strong>Algorithm</strong>.<strong>Analysis</strong>.<strong>in</strong>.C.chm::/...<br />

页码,27/49<br />

2006-1-27

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

Saved successfully!

Ooh no, something went wrong!