You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/Sorting Algorithms/Radix Sort
+8-4Lines changed: 8 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
Radix Sort
1
+
# Radix Sort
2
2
3
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
4
@@ -9,10 +9,11 @@ We can’t use counting sort because counting sort will take `O(n2)` which is wo
9
9
10
10
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.
11
11
12
-
The Radix Sort Algorithm
12
+
## The Radix Sort Algorithm
13
13
14
14
Do following for each digit i where i varies from least significant digit to the most significant digit.
15
15
Sort input array using counting sort (or any stable sort) according to the i’th digit.
16
+
16
17
Example:
17
18
18
19
Original, unsorted list:
@@ -31,13 +32,16 @@ Sorting by next digit (10s place) gives:
31
32
32
33
Sorting by the most significant digit (100s place) gives:
33
34
`2, 24, 45, 66, 75, 90, 170, 802`
34
-
What is the running time of Radix Sort?
35
+
36
+
## What is the running time of Radix Sort?
37
+
35
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.
36
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
37
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
38
41
`O(n logb(n))`. But it still doesn’t beat comparison-based sorting algorithms.
39
42
40
-
Is Radix Sort preferable to Comparison based sorting algorithms like Quick-Sort?
43
+
## Is Radix Sort preferable to Comparison based sorting algorithms like Quick-Sort?
44
+
41
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
42
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
0 commit comments