Skip to content

Commit 587a40c

Browse files
committed
docs: updated read me
1 parent c2b1bb9 commit 587a40c

File tree

1 file changed

+27
-23
lines changed
  • src/main/java/algorithms/sorting/radixSort

1 file changed

+27
-23
lines changed

src/main/java/algorithms/sorting/radixSort/README.md

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Radix Sort
22

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.
46

57
Radix Sort continuously sorts based on the least-significant segment of a element
68
to the most-significant value of a element.
@@ -11,15 +13,6 @@ Within our implementation, we define each segment as a bit chunk.
1113
For example, if we aim to sort integers, we can sort each element
1214
from the least to most significant digit, with the digits being our 'segments'.
1315

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-
2316
Within our implementation, we take the binary representation of the elements and
2417
partition it into 8-bit segments, a integer is represented in 32 bits,
2518
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
3326

3427
We place each element into a queue based on the number of possible segments that could be generated.
3528
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
3731

3832
*Source: Level Up Coding*
3933

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+
4047
## Complexity Analysis
4148
**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).
4453

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.
4856

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)*
5060

5161
## Notes
5262
- Radix sort's time complexity is dependent on the maximum number of digits in each element,
5363
hence it is ideal to use it on integers with a large range and with little digits.
5464
- This could mean that Radix Sort might end up performing worst on small sets of data
5565
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

Comments
 (0)