5
5
## Introduction
6
6
Unlike most sorting algorithms, which are based on * comparison* of whole
7
7
keys, 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
9
9
base. Using bytes (base 256) has efficiency advantages but here we use base 4
10
10
(digits 0-3) for illustration.
11
11
Straight 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,
23
23
in the data. This number defines how many iterations the algorithm will go through,
24
24
starting from the least significant (right-most) digit moving toward the most
25
25
significant (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.
27
28
28
29
### 2. Outer Loop
29
30
For each digit (scanning right to left), * Counting Sort* is performed on that
30
31
digit. This leave the array sorted.
31
32
32
33
### 3. Counting Sort
34
+
35
+ Counting sort has four parts:
36
+
33
37
* Counting occurrences of each digit (00, 01, 10 and 11 in binary)
34
38
* For each key in the array, the digit at the current position is extracted.
35
39
* 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`
47
51
48
52
## Complexity
49
53
### 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) ```
52
56
* Note that the best comparison-based sorting algorithms have ``` O(n log n) ```
53
57
complexity, but the number of digits is typically related to ``` log n ```
58
+ (otherwise there must be many duplicate keys)
54
59
so radix sorts are not necessarily better.
55
60
56
61
### Space Complexity
0 commit comments