18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

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.

Randomization<br />

Next: Bucketing Techniques Up: Approaches to Sorting Previous: Divide and Conquer<br />

Randomization<br />

Suppose we select, at random, one of the n items we seek to sort. Call this element p. In quicksort, we<br />

separate the n-1 other items into two piles: a low pile containing all the elements that will appear before p<br />

in sorted order and a high pile containing all the elements that will appear after p in sorted order. After<br />

sorting the two piles and arranging the piles correctly, we have succeeded in sorting the n items.<br />

Quicksort(A, low, high)<br />

Partition(A,low,high)<br />

if (low < high)<br />

ploc = Partition(A,low,high)<br />

Quicksort(A,low, ploc - 1)<br />

Quicksort(A, ploc+1, high)<br />

swap(A[low],A[random( )])<br />

pivot = A[low]<br />

leftwall = low<br />

file:///E|/BOOK/BOOK/NODE35.HTM (1 of 3) [19/1/2003 1:28:31]

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

Saved successfully!

Ooh no, something went wrong!