Radix Sort Chapter 10 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Radix Sort Chapter 10

Description:

... treat the keys as radix-R (i.e. base-R) numbers. Sorting Zip Codes ... How would you sort a set of zip codes in O(n) time? use 10 buckets to accumulate items ... – PowerPoint PPT presentation

Number of Views:218
Avg rating:3.0/5.0
Slides: 11
Provided by: HarryPl6
Learn more at: http://cs.calvin.edu
Category:
Tags: chapter | radix | sort | zipcodes

less

Transcript and Presenter's Notes

Title: Radix Sort Chapter 10


1
Radix Sort (Chapter 10)
  • Comparison sorting has runtime Q(n log n).
  • Can we do better?
  • To do better, we must use more information from
    the key.
  • Look at the bits.
  • Radix sort treat the keys as radix-R (i.e.
    base-R) numbers.

2
Sorting Zip Codes
  • How would you sort a set of integers in the range
    0..9 in O(n) time?
  • use 10 buckets (linked lists) to accumulate the
    items.
  • How would you sort a set of zip codes in O(n)
    time?
  • use 10 buckets to accumulate items
  • sort once for digit 5 (least significant), then
    4, then 3, then 2, then 1. (LSD sort)
  • note that each stage must be stable
  • (you can sort from left to right (MSD) if you are
    clever.)
  • Sorting machines for punched cards

3
Radix Sort Utilities
  • We need to be able to get a chunk out of a key.
  • inline int digit(int a, int digit, int radix)
    return (a / pow(radix,digit)) radix
  • It could be a fixed number of bitsoften thats
    fastest.
  • const int bitsword 32 bitschunk 4
  • const int chunksword bitsword/bitschunk
  • const int radix 1 ltlt bitschunk
  • inline int digit(long a, int b) return (a gtgt
    bitschunk(chunksword-b-1)) (radix-1)

4
Binary Quicksort
  • How about using bits as pivots for quicksort?
  • move all numbers starting with bit 0 before those
    starting with bit 1 (like a Quicksort partition
    on 10000)
  • recursively sort each half on bit 2, each quarter
    on bit 3,

5
Binary Quicksort Runtime?
  • b passes, where b is the number of bits per word
  • O(n) time per pass
  • O(b n) O(n) worst case
  • but in practice its worse than quicksort high
    constant.
  • actually, you may not even have to look at all
    the input bits!
  • but what happens if we sort many small number?

6
MSD Radix Sort
  • Like binary quicksort but with larger chunk
    size
  • But how can we move items intothe correct place
    in one pass?
  • Need to count the number of items starting with
    a, b, c, etc.
  • Then we can move items into place in one pass.
  • (Hmmm, lets seethe place for the first c word
    will be after all the a and b words)

7
MSD Radix Sort code
  • define bin(A) 1countA
  • template ltclass Itemgt
  • void radixMSD(Item a, int l, int r, int d)
  • int i, j, countR1
  • static Item auxmaxN
  • if (d gt bytesword) return
  • if (r-1 lt M) insertion(a, l, r) return
  • for (j0 jltR j) countj 0
  • for (il iltr i) countdigit(ai,d)1
  • for (j1 jltR j) countj countj-1
  • for (int il iltr i)
  • auxcountdigit(ai,d) ai
  • for (il iltr i) ai auxi-l
  • radixMSD(a, l, bin(0)-1, d1)
  • for (j0 jltR-1 j)
  • radixMSD(a, bin(j), bin(j1)-1, d1)

8
LSD Binary
  • If we work from right to left what changes?
  • Just do a stable pass for each bit!

9
LSD Radix Sort
  • Like LSD Binary sort but on bigger chunks of
    the key.
  • We still need to count the number of each type to
    figure out where to move items in one pass
  • Runtime is still O(n).

10
LSD Radix Sort -- code
  • template ltclass Itemgt
  • void radixLSD(Item a, int l, int r)
  • static Item auxmaxN
  • for (int d bytesword-1 dgt0 d--)
  • int i, j, countR1
  • for (j0 jltR j) countj 0
  • for (il iltr i) countdigit(ai,d)1
  • for (j1 jltR j) coutj countj-1
  • for (il iltr i)
  • auxcountdigit(ai,d) ai
  • for (il iltr i) ai auxi-l
Write a Comment
User Comments (0)
About PowerShow.com