Skip to content

Commit 4a6d37c

Browse files
committed
docs: readme for radix sort
1 parent e3d4b33 commit 4a6d37c

File tree

1 file changed

+40
-0
lines changed
  • src/main/java/algorithms/sorting/radixSort

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Radix Sort
2+
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+
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.
10+
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)
13+
14+
![Radix Sort](https://miro.medium.com/v2/resize:fit:661/1*xFnpQ4UNK0TvyxiL8r1svg.png)
15+
16+
*Source: Level Up Coding*
17+
18+
## Complexity Analysis
19+
**Time**:
20+
Note that we will always need to iterate through 10 different queues to rebuild our original array,
21+
and we iterate through all *w* positions, this results in:
22+
23+
- Worst case: O(w * (n + 10))
24+
- Average case: O(w * (n + 10))
25+
- Best case (sorted array): O(w * (n + 10))
26+
27+
**Space**: O(n + k)
28+
29+
## Notes
30+
- Radix sort's time complexity is dependent on the maximum number of digits in each element,
31+
hence it is ideal to use it on integers with a large range and with little digits.
32+
- This could mean that Radix Sort might end up performing worst on small sets of data
33+
if any one given element has a in-proportionate amount of digits.
34+
- Counting sort is used as a sub-routine within the Radix Sort process.
35+
36+
### Common Misconception
37+
- While not immediately obvious, we can see that radix sort is a stable sorting algorithm as
38+
they are enqueued in a manner where the first observed element will be at the head of the queue.
39+
- While it is non-comparison based, not that total ordering of elements is still required -
40+
except now this property is forced upon the algorithm in the manner of the queues.

0 commit comments

Comments
 (0)