Radix sort using queue - PowerPoint PPT Presentation

About This Presentation
Title:

Radix sort using queue

Description:

Radix sort is invented to sort piles of cards. by machines. Take a sequence of numbers, say ... of the previously sorted pile. ... – PowerPoint PPT presentation

Number of Views:277
Avg rating:3.0/5.0
Slides: 11
Provided by: p10a
Category:
Tags: pile | queue | radix | sort | using

less

Transcript and Presenter's Notes

Title: Radix sort using queue


1
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.
2
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
3
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! Thats
why we use queue.
4
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!
5
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
329
497
6
Then we get every number out of the bins in an
orderly manner starting with bin0 to bin9.
Numbers from each bin is taken out using the
First-in-first-out (FIFO) policy.
450
720
411
329
497
720 450
411
497
329
target
This completes the first pass.
7
We then process the second digit using the same
procedure.
411
720 329
497
450
target
Repeat the same procedure on the next unsorted
digit until every digit is sorted.
8
Technicalities
4) similarly, use bins1.Dequeue() to get one
number out of bin1.
9
Radix sort using queues
An example of sorting 2-digit integers.
void RadixSort(int target, int size, int
n_digits) Queueltintgt bins10 coutltlt"in
Radix."ltltendl n_digits2 for(int d1
dltn_digits d) int pos int tmp
coutltlt"Sorting the "ltltdltlt"-digit"ltltendl
for(int i0 iltsize i) int
tmptargeti pos( (d1)? tmp10 tmp/10
) binspos.Enqueue(tmp) //for each item
in target int j0 for(int bin_num0
bin_numlt10 bin_num) while(!binsbin_num.I
sEmpty()) targetjbinsbin_num.Dequeue()

10
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.
Write a Comment
User Comments (0)
About PowerShow.com