55## Introduction
66Unlike most sorting algorithms, which are based on * comparison* of whole
77keys, radix sorting is based on examining individual "digits" in the
8- keys. For integers (used here) this could be decimal digits (base 10), bits (base 2) or and other
8+ keys. For integers (used here) this could be decimal digits (base 10), bits (base 2) or any other
99base. Using bytes (base 256) has efficiency advantages but here we use base 4
1010(digits 0-3) for illustration.
1111Straight Radix Sort (also known as Least Significant Digit Radix Sort)
@@ -23,13 +23,17 @@ The ```Radixsort``` function begins by determining the maximum number of digits,
2323in the data. This number defines how many iterations the algorithm will go through,
2424starting from the least significant (right-most) digit moving toward the most
2525significant (left-most). Here we use base 4 digits (0-3 or two bits)
26- and show values in binary.
26+ and show values in decimal, base 4 (the most helpful in understanding
27+ the algorithm) and binary.
2728
2829### 2. Outer Loop
2930For each digit (scanning right to left), * Counting Sort* is performed on that
3031digit. This leave the array sorted.
3132
3233### 3. Counting Sort
34+
35+ Counting sort has four parts:
36+
3337* Counting occurrences of each digit (00, 01, 10 and 11 in binary)
3438 * For each key in the array, the digit at the current position is extracted.
3539 * The algorithm counts how many times each digit occurs.
@@ -47,10 +51,11 @@ the keys sorted on the current digit, is copied back to the original array ```A`
4751
4852## Complexity
4953### Time Complexity
50- * Each iteration uses ``` Countingsort ``` , which requires ``` O(n) ``` , where n is the number of elements.
51- * If we * assume the number of digits is limited * the overall time complexity is: ``` O(n) ```
54+ * Each iteration uses ``` Countingsort ``` , which takes ``` O(n) ``` time , where n is the number of elements.
55+ * If we * assume the maximum number of digits is fixed * the overall time complexity is: ``` O(n) ```
5256* Note that the best comparison-based sorting algorithms have ``` O(n log n) ```
5357 complexity, but the number of digits is typically related to ``` log n ```
58+ (otherwise there must be many duplicate keys)
5459 so radix sorts are not necessarily better.
5560
5661### Space Complexity
0 commit comments