Download presentation
Presentation is loading. Please wait.
Published byNathaniel Watson Modified over 9 years ago
2
32 90 56 88 19 00 51 13 72 88 1200 12 13 19 32 51 56 72 88 88 90 Warning! Too much programming is not healthy.
3
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 497 450 411 329 457 450 411 720 329 411 457 450 720 329 410 451 457 720
4
497 450 411 329 720 450 411 497 329 411 720 329 450 497 329 411 450 497 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: Notice that if you look only at the four hundreds,the numbers are already sorted! It is important that we keep the order of the previously sorted pile. 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.
5
Given: an array target[] of integers of n_digits Use: QueueLL package and Radix sort algorithm Goal: sort the integers in target[] in ascending order Our mission : This slide will self-destruct in 5 secs. Good luck! #include “QueueLL.cpp”
6
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 #. #0 #1#2#3#4#5#6#7#8#9 720 450 411329497 Using the same set of numbers, we first sort the least significant digit by putting the numbers ending with k into bin#k. 720 497 450 411 329 target [] Imagine that the numbers in each bin forms a queue.
7
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. #0 #1#2#3#4#5#6#7#8#9 720 450 411329497 720 450411497329 target[] This completes the first pass.
8
We then process the second digit using the same procedure. #0 #1#2#3#4#5#6#7#8#9 411411 720720 329329 450450497497 411720 329497450 target[] Repeat the same procedure on the next unsorted digit until every digit is sorted.
9
Technicalities: 1)The bins are implemented using arrays of queues, and each queue contains integers. #0 #1 #8#9... Queue bins[10]; bins[1] represents bin#1 #1 2) 3) to put a number (411) into bin#1 #1 411 bins[1].Enqueue(411); 4) similarly, use bins[1].Dequeue() to get one number out of bin#1.
10
Put numbers back to target[]. Put numbers into the right bins. Radix sort using queues void RadixSort(int target[], int size, int n_digits){ Queue bins[10]; cout<<"in Radix."<<endl; n_digits=2; for(int d=1; d<=n_digits; d++) { int pos; int tmp; cout<<"Sorting the "<<d<<"-digit"<<endl; for(int i=0; i<size; i++){ int tmp=target[i]; pos=( (d==1)? tmp%10: tmp/10 ); bins[pos].Enqueue(tmp); }//for each item in target[] int j=0; for(int bin_num=0; bin_num<10; bin_num++) while(!bins[bin_num].IsEmpty()) target[j++]=bins[bin_num].Dequeue(); } An example of sorting 2-digit integers. This gets the wanted digit out of the number. If you want to generalize the function to handle integers of any given number of digits, you only need to modify this line. Repeat for every digit
11
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.