12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

Sec. 7.7 Binsort <strong>and</strong> Radix Sort 259over the time required <strong>to</strong> find the k largest elements using one of the other sortingmethods described earlier. One situation where we are able <strong>to</strong> take advantage of thisconcept is in the implementation of Kruskal’s minimum-cost spanning tree (MST)algorithm of Section 11.5.2. That algorithm requires that edges be visited in ascendingorder (so, use a min-heap), but this process s<strong>to</strong>ps as soon as the MST iscomplete. Thus, only a relatively small fraction of the edges need be sorted.7.7 Binsort <strong>and</strong> Radix SortImagine that for the past year, as you paid your various bills, you then simply piledall the paperwork on<strong>to</strong> the <strong>to</strong>p of a table somewhere. Now the year has ended <strong>and</strong>its time <strong>to</strong> sort all of these papers by what the bill was for (phone, electricity, rent,etc.) <strong>and</strong> date. A pretty natural approach is <strong>to</strong> make some space on the floor, <strong>and</strong> asyou go through the pile of papers, put the phone bills in<strong>to</strong> one pile, the electric billsin<strong>to</strong> another pile, <strong>and</strong> so on. Once this initial assignment of bills <strong>to</strong> piles is done (inone pass), you can sort each pile by date relatively quickly because they are eachfairly small. This is the basic idea behind a Binsort.Section 3.9 presented the following code fragment <strong>to</strong> sort a permutation of thenumbers 0 through n − 1:for (i=0; i

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

Saved successfully!

Ooh no, something went wrong!