1
1
# Radix Sort
2
2
3
3
Radix Sort is a non-comparison based, stable sorting algorithm.
4
- We first treat each element as a string with * w* digits (Padding elements that have
5
- less than * w* digits).
6
4
7
- From the least-significant digit to the most-significant digit, we constantly
8
- split them into ten queues corresponding to the number range * [ 0, 9] * . We then move
9
- through the queue and concatenate the elements back into a list at the next iteration.
5
+ Radix Sort continuously sorts based on the least-significant segment of a element
6
+ to the most-significant value of a element.
10
7
11
- This takes advantage of the concept of place value.
12
- (The value of a digit in a number relative to its position within the number)
8
+ The definition of a 'segment' is user defined and defers from implementation to implementation.
9
+ Within our implementation, we define each segment as a bit chunk.
10
+
11
+ For example, if we aim to sort integers, we can sort each element
12
+ from the least to most significant digit, with the digits being our 'segments'.
13
+
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
+ Within our implementation, we take the binary representation of the elements and
24
+ partition it into 8-bit segments, a integer is represented in 32 bits,
25
+ this gives us 4 total segments to sort through.
26
+
27
+ Note that the binary representation is weighted positional,
28
+ where each bit's value is dependent on its overall position
29
+ within the representation (the n-th bit from the right represents * 2^n* ),
30
+ hence we can actually increase / decrease the number segments we wish to conduct a split from.
13
31
14
32
![ Radix Sort] ( https://miro.medium.com/v2/resize:fit:661/1*xFnpQ4UNK0TvyxiL8r1svg.png )
15
33
34
+ We place each element into a queue based on the number of possible segments that could be generated.
35
+ Suppose the values of our segments are in base-10, (limited to a value within range * [ 0, 9] * ),
36
+ we get 10 queues.
37
+
16
38
* Source: Level Up Coding*
17
39
18
40
## Complexity Analysis
19
41
** Time** :
20
- Let * k* be the base of the number system being used. For integers in java, it is base 10
21
- (we have digits 0 to 9), in the case of base 2 (binary), we only have 2 digits (1, 0),
22
- base 3, (0, 1, 2). Hence, we can see that the base of the number system determines the number of
23
- different queues we need to iterate through to rebuild our original array, additionally, we also
24
- need to iterate through all * w* positions (digits), this results in the following complexities:
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.
25
44
26
45
- Worst case: O(w * (n + k))
27
46
- Average case: O(w * (n + k))
@@ -39,7 +58,4 @@ Radix Sort process.
39
58
40
59
### Common Misconception
41
60
- While not immediately obvious, we can see that radix sort is a stable sorting algorithm as
42
- they are enqueued in a manner where the first observed element remains at the head of the queue.
43
- - While it is non-comparison based, not that total ordering of elements is still required -
44
- except now this property is forced upon the algorithm in the manner of how the queues
45
- are structured.
61
+ they are enqueued in a manner where the first observed element remains at the head of the queue.
0 commit comments