Вы находитесь на странице: 1из 10

32 90 56 88 19 00 51 13 72 88 12 00 12 13 19 32 51 56 72 88 88 90

Warning! Too much programming is not healthy.


In the “ancient time,” computer programs are stored on
punch cards. Radix sort is invented to sort piles of cards
by machines.

Take a sequence of numbers, say


720, 497, 450, 411, 329

one would probably start out sorting from the most significant
digit, and descend to the least significant digit:
(human sorter)

720 329 329 329


497 457 411 410
450 450 457 451
411 411 450 457
329 720 720 720
Idea of Radix sort:
instead of sorting from the most significant
digit, we start out sorting the least significant digit.

Using the same set of numbers, we would have:

720 720 411 329


497 450 720 411
450 411 329 450
411 497 450 497
329 329 497 720

It is important that we keep the order


Notice that if you look only at the of the previously sorted pile.
four hundreds,the numbers are
already sorted! If we destroy the order of the four
hundreds when sorting the last digit,
we would not get the correct sorting!

That’s why we use queue.


Our mission:

Given: an array target[] of integers of n_digits

Use: QueueLL package and #include “QueueLL.cpp”


Radix sort algorithm

Goal: sort the integers in target[] in ascending order

This slide will self-destruct in 5 secs. Good luck!


Imagine that we are sorting punch cards.
We start out creating 10 bins (bin #0, to bin #9), each bin holds
the numbers whose digit number (under sorting) match the bin #.

Using the same set of numbers, 720 target []


we first sort the least significant digit 497
by putting the numbers ending with k 450
411
into bin#k.
329

450
720 411 497 329

#0 #1 #2 #3 #4 #5 #6 #7 #8 #9

Imagine that the numbers in each bin


forms a queue.
Then we get every number out of the bins in an orderly manner: starting
with bin#0 to bin#9. Numbers from each bin is taken out using the
First-in-first-out (FIFO) policy.

450
720 411 497 329

#0 #1 #2 #3 #4 #5 #6 #7 #8 #9

720 450 411 497 329 target[] This completes the first pass.
We then process the second digit using the same procedure.

329
411 720 450 497

#0 #1 #2 #3 #4 #5 #6 #7 #8 #9

411 720 329 450 497


target[]

Repeat the same procedure on the next unsorted digit until every digit is sorted.
Technicalities:
1)The bins are implemented using arrays of queues,
and each queue contains integers.

... Queue<int> bins[10];


#0 #1 #8 #9

2)
bins[1] represents bin#1
#1

3) to put a number (411) into bin#1

411
bins[1].Enqueue(411);
#1
4) similarly, use bins[1].Dequeue() to get one number out of bin#1.
Radix sort using queues An example of sorting 2-digit integers.

void RadixSort(int target[], int size, int n_digits){

Queue<int> bins[10];

cout<<"in Radix."<<endl; Repeat for every digit


n_digits=2;

for(int d=1; d<=n_digits; d++)


{
int pos; Put numbers into the right bins.
int tmp;

cout<<"Sorting the "<<d<<"-digit"<<endl;


This gets the wanted digit
for(int i=0; i<size; i++){
int tmp=target[i]; out of the number. If you want to
generalize the function to handle
pos=( (d==1)? tmp%10: tmp/10 );
bins[pos].Enqueue(tmp); integers of any given number of digits,
}//for each item in target[]
you only need to modify this line.
int j=0;

for(int bin_num=0; bin_num<10; bin_num++)


while(!bins[bin_num].IsEmpty())
target[j++]=bins[bin_num].Dequeue();
}
}
Put numbers back to target[].
Radix sort using queues

Finally, it is important to know that radix sort


is linear, where as selection sort is quadratic.

This will be the discussion for our next section.

Вам также может понравиться