11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

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 247First pass(on right digit)Initial List: 27 91 1 97 17 23 84 28 72 5 67 25Second pass(on left digit)001 5191 1117272223 2527 283233484455 255666772797 17 677728288849991 97Result of first pass: 91 1 72 23 84 5 25 27 97 17 67 28Result of second pass: 1 5 17 23 25 27 28 67 72 84 91 97Figure 7.17 An example of Radix Sort for twelve two-digit numbers in base ten.Two passes are required to sort the list.a large key range requires an unacceptably large array B. Thus, even the extendedBinsort is useful only for a limited key range.A further generaliz<strong>at</strong>ion to Binsort yields a bucket sort. Each bin is associ<strong>at</strong>edwith not just one key, but r<strong>at</strong>her a range of key values. A bucket sort assigns recordsto bins <strong>and</strong> then relies on some other sorting technique to sort the records withineach bin. The hope is th<strong>at</strong> the rel<strong>at</strong>ively inexpensive bucketing process will putonly a small number of records in each bin, <strong>and</strong> th<strong>at</strong> a “cleanup sort” within thebins will then be rel<strong>at</strong>ively cheap.There is a way to keep the number of bins <strong>and</strong> the rel<strong>at</strong>ed processing smallwhile allowing the cleanup sort to be based on Binsort. Consider a sequence ofrecords with keys in the range 0 to 99. If we have ten bins available, we can firstassign records to bins by taking their key value modulo 10. Thus, every key willbe assigned to the bin m<strong>at</strong>ching its rightmost decimal digit. We can then take theserecords from the bins in order <strong>and</strong> reassign them to the bins on the basis of theirleftmost (10’s place) digit (define values in the range 0 to 9 to have a leftmost digitof 0). In other words, assign the ith record from array A to a bin using the formulaA[i]/10. If we now g<strong>at</strong>her the values from the bins in order, the result is a sortedlist. Figure 7.17 illustr<strong>at</strong>es this process.

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

Saved successfully!

Ooh no, something went wrong!