2
2
3
3
## Background
4
4
5
- Radix Sort is a non-comparison based, stable sorting algorithm with a counting sort subroutine.
5
+ Radix Sort is a non-comparison based, stable sorting algorithm that conventionally uses counting sort as a subroutine.
6
6
7
- Radix Sort continuously sorts based on the least-significant segment of a element
8
- to the most-significant value of a element .
7
+ Radix Sort performs counting sort several times on the numbers. It sorts starting with the least-significant segment
8
+ to the most-significant segment .
9
9
10
+ ### Segments
10
11
The definition of a 'segment' is user defined and defers from implementation to implementation.
11
- Within our implementation, we define each segment as a bit chunk.
12
+ It is most commonly defined as a bit chunk.
12
13
13
14
For example, if we aim to sort integers, we can sort each element
14
15
from the least to most significant digit, with the digits being our 'segments'.
15
16
16
17
Within our implementation, we take the binary representation of the elements and
17
- partition it into 8-bit segments, a integer is represented in 32 bits,
18
+ partition it into 8-bit segments. An integer is represented in 32 bits,
18
19
this gives us 4 total segments to sort through.
19
20
20
- Note that the binary representation is weighted positional,
21
- where each bit's value is dependent on its overall position
22
- within the representation (the n-th bit from the right represents * 2^n* ),
23
- hence we can actually increase / decrease the number segments we wish to conduct a split from.
21
+ Note that the number of segments is flexible and can range up to the number of digits in the binary representation.
22
+ (In this case, sub-routine sort is done on every digit from right to left)
24
23
25
24
![ Radix Sort] ( https://miro.medium.com/v2/resize:fit:661/1*xFnpQ4UNK0TvyxiL8r1svg.png )
26
25
@@ -46,22 +45,23 @@ Hence, a stable sort is required to maintain the order as
46
45
the sorting is done with respect to each of the segments.
47
46
48
47
## Complexity Analysis
48
+ Let b-bit words be broken into r-bit pieces. Let n be the number of elements to sort.
49
49
50
- ** Time** :
51
- Let * b* be the length of a single element we are sorting and * r* is the amount of bit-string
52
- we plan to break each element into.
53
- (Essentially, * b/r* represents the number of segments we
54
- sort on and hence the number of passes we do during our sort).
50
+ * b/r* represents the number of segments and hence the number of counting sort passes. Note that each pass
51
+ of counting sort takes * (2^r + n)* (O(k+n) where k is the range which is 2^r here).
55
52
56
- Note that we derive * (2^r + n)* from the counting sort subroutine,
57
- since we have * 2^r* represents the range since we have * r* bits.
58
-
59
- We get a general time complexity of * O((b/r) * (2^r + n))*
53
+ ** Time** : * O((b/r) * (2^r + n))*
60
54
61
55
** Space** : * O(n + 2^r)*
62
56
63
- ## Notes
57
+ ### Choosing r
58
+ Previously we said the number of segments is flexible. Indeed, it is but for more optimised performance, r needs to be
59
+ carefully chosen. The optimal choice of r is slightly smaller than logn which can be justified with differentiation.
64
60
61
+ Briefly, r=lgn --> Time complexity can be simplified to (b/lgn)(2n). <br >
62
+ For numbers in the range of 0 - n^m, b = mlgn and so the expression can be further simplified to * O(mn)* .
63
+
64
+ ## Notes
65
65
- Radix sort's time complexity is dependent on the maximum number of digits in each element,
66
66
hence it is ideal to use it on integers with a large range and with little digits.
67
67
- This could mean that Radix Sort might end up performing worst on small sets of data
0 commit comments