Radix Sort
Radix Sort
Sagar Virani
Assistant Professor
Computer Engineering
Department
• Example:
• The decimal number system has 10 digits, so the radix is 10.
• The octal number system has 8 digits, so the radix is 8.
Radix Sort
• The idea of Radix Sort is to do digit by digit( or character by
character) sort starting from least significant digit to most significant
digit.
• Radix sort uses counting sort or bucket sort as a subroutine to sort.
Any stable sorting algorithm can be used.
Radix Sort
329
457
657
839
436
720
355
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
329
457
657
839
436
720
355
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
329
457
657
839
436
720
355
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
329 720
457
657
839
436
355
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
329 720
457
657
839
436
355
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
329 720
457 355
657
839
436
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
329 720
457 355
657
839
436
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
329 720
457 355
657 436
839
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
329 720
457 355
657 436
839
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
329 720
355
657 436
839 457
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
329 720
355
657 436
839 457
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
329 720
355
436
839 457
657
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
329 720
355
436
839 457
657
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
720
355
436
839 457
657
329
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
LSB
720
355
436
839 457
657
329
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
720
355
436
457
657
329
839
Radix Sort
• Consider the Least Significant Bit (LSB) and perform stable sorting on it.
720
355
436
457
657
329
839
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
355
436
457
657
329
839
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
355
436
457
657
329
839
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
355
436
457
657
329
839
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
355
436
457
657
329
839
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
355 329
436
457
657
839
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
355 329
436
457
657
839
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
355 329
436
457
657
839
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
355 329
436
457
657
839
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
355 329
436
457 839
657
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
355 329
436
457 839
657
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
329
436
457 839
657 355
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
329
436
457 839
657 355
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
329
436
839
657 355
457
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
LSB
720
329
436
839
657 355
457
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
720
329
436
839
355
457
657
Radix Sort
• Consider the Second Least Significant Bit (LSB) and perform stable
sorting on it.
720
329
436
839
355
457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
720
329
436
839
355
457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
720
329
436
839
355
457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
720 329
436
839
355
457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
720 329
436
839
355
457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
720 329
355
436
839
457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
720 329
355
436
839
457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
720 329
355
436
839
457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
720 329
355
436
839
457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
720 329
355
436
839 457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
720 329
355
436
839 457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
720 329
355
436
839 457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
720 329
355
436
839 457
657
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
329
355
436
839 457
657
720
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
MSB
329
355
436
839 457
657
720
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
329
355
436
457
657
720
839
Radix Sort
• Consider the Most Significant Bit (MSB) and perform stable sorting on
it.
329
355
436
457
657
720
839
Sorted!!!
Radix Sort
Algorithm
RADIX-SORT(A, d)
for i = 1 to d
use a stable sort to sort array A on digit i
Analysis
• The running time of RADIX-SORT is d times the running time of stable
sorting algorithm used.
• If counting sort algorithm is used, it takes θ(n + k) time to sort.
• Therefore, running time of RADIX-SORT is T(n) = θ(d(n + k)).
k = the base or number of buckets
n = no of elements to be sorted
d = length of each element
Radix Sort
• Only applies to integers, fixed size strings, floating points and to "less
than", "greater than" or "lexicographic order" comparison predicates,
whereas comparison sorts accommodates different orders.
• May require additional memory space to sort digits, especially when
implemented using counting sort.
Is Radix Sort preferable to Comparison based
sorting algorithms like Quick-Sort?
• If we have log2n bits for every digit, the running time of Radix
appears to be better than Quick Sort for a wide range of input
numbers.
• The constant factors hidden in asymptotic notation are higher for
Radix Sort and Quick-Sort uses hardware caches more effectively.
• Also, Radix sort uses counting sort as a subroutine and counting sort
takes extra space to sort numbers.
Pseudo code for Radix sort Algorithm
RADIX-SORT (A, d)
{
for i 1 to d do
COUNTING-SORT(A, i)
done
}
Any Questions?