8000 Merge pull request #64 from abhishek7553/patch-1 · JSSpagh/Algorithms-Explanation@ac9c038 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac9c038

Browse files
authored
Merge pull request TheAlgorithms#64 from abhishek7553/patch-1
Radix Sort
2 parents 2c7f2c7 + 7edab6b commit ac9c038

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

en/Sorting Algorithms/Radix Sort.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Radix Sort
2+
3+
The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc) is `Ω(nlogn)`, i.e., they cannot do better than `nlogn`.
4+
5+
Counting sort is a linear time sorting algorithm that sort in `O(n+k)` time when elements are in the range from 1 to k.
6+
7+
What if the elements are in the range from 1 to n2? We can’t use counting sort because counting sort will take `O(n2)` which is worse than comparison-based sorting algorithms. Can we sort such an array in linear time?
8+
9+
Radix Sort is the answer. The idea of Radix Sort is to do digit by digit sort starting from least significant digit to most significant digit. Radix sort uses counting sort as a subroutine to sort.
10+
11+
## The Radix Sort Algorithm
12+
13+
Do following for each digit i where i varies from least significant digit to the most significant digit.
14+
Sort input array using counting sort (or any stable sort) according to the i’th digit.
15+
16+
Example:
17+
18+
Original, unsorted list:
19+
`170, 45, 75, 90, 802, 24, 2, 66`
20+
21+
Sorting by least significant digit (1s place) gives:
22+
23+
[*Notice that we keep 802 before 2, because 802 occurred
24+
before 2 in the original list, and similarly for pairs
25+
170 & 90 and 45 & 75.]
26+
27+
Sorting by next digit (10s place) gives:
28+
29+
[*Notice that 802 again comes before 2 as 802 comes before 2 in the previous list.]
30+
31+
`802, 2, 24, 45, 66, 170, 75, 90`
32+
33+
Sorting by the most significant digit (100s place) gives:
34+
`2, 24, 45, 66, 75, 90, 170, 802`
35+
36+
## What is the running time of Radix Sort?
37+
38+
Let there be d digits in input integers. Radix Sort takes `O(d*(n+b))` time where b is the base for representing numbers, for example, for the decimal system, b is 10.
39+
What is the value of d? If `k` is the maximum possible value, then d would be `O(logb(k))`. So overall time complexity is `O((n+b) * logb(k))`. Which looks more than the
40+
time complexity of comparison-based sorting algorithms for a large k. Let us first limit k. Let k <= nc where c is a constant. In that case, the complexity becomes
41+
`O(n logb(n))`. But it still doesn’t beat comparison-based sorting algorithms.
42+
43+
## Is Radix Sort preferable to Comparison based sorting algorithms like Quick-Sort?
44+
45+
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
46+
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
47+
takes extra space to sort numbers.
48+
49+
Video reference: https://youtu.be/nu4gDuFabIM

0 commit comments

Comments
 (0)
0