Radix sort using queue
Download
1 / 10

32 90 56 88 19 00 51 13 72 88 12 - PowerPoint PPT Presentation


  • 148 Views
  • Uploaded on

Radix sort using queue. 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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about '32 90 56 88 19 00 51 13 72 88 12' - ryder


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Radix sort using queue

Radix sort using queue

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.


Radix sort using queue

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)

329

457

450

411

720

329

411

457

450

720

329

410

451

457

720

720

497

450

411

329


Radix sort using queue

720

450

411

497

329

Notice that if you look only at the four hundreds,the numbers are already sorted!

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:

329

411

450

497

720

411

720

329

450

497

720

497

450

411

329

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.


Radix sort using queue

Our mission:

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

#include “QueueLL.cpp”

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


Radix sort using queue

Using the same set of numbers,

target []

720

497

450

411

329

we first sort the least significant digit

by putting the numbers ending with k

into bin#k.

#1

#2

#3

#4

#5

#6

#7

#8

#9

#0

Imagine that the numbers in each bin

forms a queue.

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 #.

450

720

411

497

329


Radix sort using queue

#1

#2

#3

#4

#5

#6

#7

#8

#9

#0

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

720 450

411

497

329

target[]

This completes the first pass.


Radix sort using queue

329

720

450

497

411

#1

#2

#3

#4

#5

#6

#7

#8

#9

#0

We then process the second digit using the same procedure.

411

720 329

450

497

target[]

Repeat the same procedure on the next unsorted digit until every digit is sorted.


Radix sort using queue

1)The bins are implemented using arrays of queues,

and each queue contains integers.

...

Queue<int> bins[10];

#1

#0

#8

#9

2)

bins[1] represents bin#1

#1

#1

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

411

bins[1].Enqueue(411);

Technicalities:

4) similarly, use bins[1].Dequeue()to get one number out of bin#1.


Radix sort using queues

Repeat for every digit

Put numbers into the right bins.

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.

Put numbers back to target[].

An example of sorting 2-digit integers.

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

Queue<int> 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();

}

}

Radix sort using queues


Radix sort using queue

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.