23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

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.

Perform<strong>in</strong>g Quick-Sort on Arrays <strong>and</strong> Lists<br />

In Code Fragment 11.5, we give a pseudo-code description of the quick-sort<br />

algorithm that is efficient for sequences implemented as arrays or l<strong>in</strong>ked lists. The<br />

algorithm follows the template for quick-sort given above, add<strong>in</strong>g the detail of<br />

scann<strong>in</strong>g the <strong>in</strong>put sequence S backwards to divide it <strong>in</strong>to the lists L, E, <strong>and</strong> G of<br />

elements that are respectively less than, equal to, <strong>and</strong> greater than the pivot. We<br />

perform this scan backwards, s<strong>in</strong>ce remov<strong>in</strong>g the last element <strong>in</strong> a sequence is a<br />

constant-time operation <strong>in</strong>dependent of whether the sequence is implemented as an<br />

array or a l<strong>in</strong>ked list. We then recur on the L <strong>and</strong> G lists, <strong>and</strong> copy the sorted lists L,<br />

E, <strong>and</strong> G back to S We perform this latter set of copies <strong>in</strong> the forward direction,<br />

699

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

Saved successfully!

Ooh no, something went wrong!