1
1
# Radix Sort
2
2
3
- Radix Sort is a non-comparison based, stable sorting algorithm.
3
+ ## Background
4
+
5
+ Radix Sort is a non-comparison based, stable sorting algorithm with a counting sort subroutine.
4
6
5
7
Radix Sort continuously sorts based on the least-significant segment of a element
6
8
to the most-significant value of a element.
@@ -11,15 +13,6 @@ Within our implementation, we define each segment as a bit chunk.
11
13
For example, if we aim to sort integers, we can sort each element
12
14
from the least to most significant digit, with the digits being our 'segments'.
13
15
14
- While Radix Sort is non-comparison based,
15
- the that total ordering of elements is still required.
16
- This is maintained in how the states are stored after sorting is conducted with respect to each digit.
17
-
18
- This total ordering is needed because once we assigned a element to a order based on a segment,
19
- the order * cannot* change unless deemed by a segment with a higher significance.
20
- Hence a stable sort is required to maintain the order as
21
- the sorting is done with respect to each of the segments.
22
-
23
16
Within our implementation, we take the binary representation of the elements and
24
17
partition it into 8-bit segments, a integer is represented in 32 bits,
25
18
this gives us 4 total segments to sort through.
@@ -33,29 +26,40 @@ hence we can actually increase / decrease the number segments we wish to conduct
33
26
34
27
We place each element into a queue based on the number of possible segments that could be generated.
35
28
Suppose the values of our segments are in base-10, (limited to a value within range * [ 0, 9] * ),
36
- we get 10 queues.
29
+ we get 10 queues. We can also see that radix sort is stable since
30
+ they are enqueued in a manner where the first observed element remains at the head of the queue
37
31
38
32
* Source: Level Up Coding*
39
33
34
+ ### Implementation Invariant
35
+ At the start of the * i-th* segment we are sorting on, the array has already been sorted on the
36
+ previous * (i - 1)-th* segments.
37
+
38
+ ### Common Misconceptions
39
+
40
+ While Radix Sort is non-comparison based,
41
+ the that total ordering of elements is still required.
42
+ This total ordering is needed because once we assigned a element to a order based on a segment,
43
+ the order * cannot* change unless deemed by a segment with a higher significance.
44
+ Hence, a stable sort is required to maintain the order as
45
+ the sorting is done with respect to each of the segments.
46
+
40
47
## Complexity Analysis
41
48
** Time** :
42
- Let * w* be the number of segments we sort through, * n* be the number of elements
43
- and * k* be the number of queues.
49
+ Let * b* be the length of a single element we are sorting and * r* is the amount of bit-string
50
+ we plan to break each element into.
51
+ (Essentially, * b/r* represents the number of segments we
52
+ sort on and hence the number of passes we do during our sort).
44
53
45
- - Worst case: O(w * (n + k))
46
- - Average case: O(w * (n + k))
47
- - Best case (sorted array): O(w * (n + k))
54
+ Note that we derive * (2^r + n)* from the counting sort subroutine,
55
+ since we have * 2^r* represents the range since we have * r* bits.
48
56
49
- ** Space** : O(n + k)
57
+ We get a general time complexity of * O((b/r) * (2^r + n))*
58
+
59
+ ** Space** : * O(n + 2^r)*
50
60
51
61
## Notes
52
62
- Radix sort's time complexity is dependent on the maximum number of digits in each element,
53
63
hence it is ideal to use it on integers with a large range and with little digits.
54
64
- This could mean that Radix Sort might end up performing worst on small sets of data
55
65
if any one given element has a in-proportionate amount of digits.
56
- - It is interesting to note that counting sort is used as a sub-routine within the
57
- Radix Sort process.
58
-
59
- ### Common Misconception
60
- - While not immediately obvious, we can see that radix sort is a stable sorting algorithm as
61
- they are enqueued in a manner where the first observed element remains at the head of the queue.
0 commit comments