@@ -22,7 +22,7 @@ would be good for the counts array.
22
22
Main
23
23
Radixsort(A, n) // Sort array A[1]..A[n] in ascending order. \\B 1
24
24
25
- Find maximum number of "digits" used in the data
25
+ Find maximum number of "digits" used in the data \\B 2
26
26
\\Expl{ This depends on the radix (base) we use to view the data.
27
27
We could use radix 10 (decimal digits), radix 2
28
28
(binary) or anything else. Here we use radix 4 for illustration
@@ -31,7 +31,7 @@ Radixsort(A, n) // Sort array A[1]..A[n] in ascending order. \\B 1
31
31
word size rather than scanning all the input data as we do here.
32
32
\\Expl}
33
33
34
- for each digit k up to maximum digit number
34
+ for each digit k up to maximum digit number \\B 3
35
35
\\Expl{ We scan the digits right to left, from least significant to
36
36
most significant.
37
37
\\Expl}
@@ -46,31 +46,11 @@ Radixsort(A, n) // Sort array A[1]..A[n] in ascending order. \\B 1
46
46
// Done \\B 11
47
47
\\Code}
48
48
49
- \\Code{
50
- MaximumBit
51
- \\Note{ Skip this
52
- \\Note}
53
- maxNumber <- max(A) \\B 2
54
- maxBit <- 0
55
- while maxNumber > 0
56
- \\In{
57
- maxNumber <- maxNumber/2
58
- maxBit <- maxBit+1
59
- \\In}
60
- \\Code}
61
-
62
- \\Code{
63
- RSFor
64
- \\Note{ Skip this
65
- \\Note}
66
- for k <- 0 to maxDigit \\B 3
67
- \\Code}
68
-
69
49
\\Code{
70
50
Countingsort
71
51
// Countingsort(A, k, n) \\B 4
72
- Count number of 1s and 0s in B \\Ref CountNums
73
- Array B <- counts or each kth digit value \\Ref CountNums
52
+ // Count number of 1s and 0s in B
53
+ Array C <- counts of each kth digit value \\Ref CountNums
74
54
\\Expl{ We count the number of occurrences of each digit value (0-3
75
55
here) in the kth digits of the data.
76
56
\\Expl}
@@ -79,11 +59,11 @@ Cumulatively sum digit value counts \\Ref CumSum
79
59
plus all smaller digit values. This allows us to determine where the
80
60
last occurrence of each digit value will appear in the sorted array.
81
61
\\Expl}
82
- Populate temporary array C with sorted numbers \\Ref Populate
83
- \\Expl{ We copy the data to temporary array C , using the digit
62
+ Populate temporary array B with sorted numbers \\Ref Populate
63
+ \\Expl{ We copy the data to temporary array B , using the digit
84
64
value counts to determine where each element is copied to.
85
65
\\Expl}
86
- Copy C back to A \\B 10
66
+ Copy B back to A \\B 10
87
67
\\Expl{ Array A is now sorted on digit k and all smaller digits
88
68
(because the smaller digits were sorted previously and counting
89
69
sort is stable).
@@ -92,11 +72,11 @@ Copy C back to A \\B 10
92
72
93
73
\\Code{
94
74
CountNums
95
- // Put counts of each kth digit value in array B \\B 5
96
- initialise array B to all zeros
97
- for num in A
75
+ // Put counts of each kth digit value in array C \\B 5
76
+ initialise array C to all zeros \\B 16
77
+ for num in A \\B 13
98
78
\\In{
99
- digit <- kth digit value in num
79
+ digit <- kth digit value in num \\B 17
100
80
\\Expl{ To extract the kth digit we can use div and mod operations.
101
81
If the radix is a power of two we can use bit-wise operations
102
82
(right shift and bit-wise and) instead.
@@ -106,41 +86,34 @@ for num in A
106
86
highlighted, and the digit value 0-3 (maybe the latter can be done
107
87
by just highlighting B[digit] instead).
108
88
\\Note}
109
- B [digit] <- B [digit]+1
89
+ C [digit] <- C [digit]+1 \\B 12
110
90
\\In}
111
91
\\Code}
112
92
113
- \\Code{
114
- KthBit
115
- \\Note{ Skip this
116
- \\Note}
117
- bit <- (num & (1 << i)) >> i
118
- \\Code}
119
-
120
93
\\Code{
121
94
CumSum
122
95
// Cumulatively sum counts \\B 6
123
96
\\Note{ Best remove this comment line and move bookmark
124
97
\\Note}
125
- for i = 1 to maximum digit value
98
+ for i = 1 to maximum digit value \\B 14
126
99
\\Expl{ We must scan left to right. The count for digit 0 remains
127
100
unchanged.
128
101
\\Expl}
129
102
\\In{
130
- B[i] = B[i-1] + B[i]
103
+ B[i] = B[i-1] + B[i] \\B 15
131
104
\\In}
132
105
\\Code}
133
106
134
107
\\Code{
135
108
Populate
136
109
// Populate new array C with sorted numbers \\B 7
137
- for each num in A in reverse order
110
+ for each num in A in reverse order \\B 8
138
111
\\Expl{ We go from right to left so that we preserve the order of numbers
139
112
with the same digit.
140
113
This is CRUCIAL in radix sort as the counting sort MUST be stable.
141
114
\\Expl}
142
115
\\In{
143
- digit <- kth digit value in num \\Ref KthBit
116
+ digit <- kth digit value in num \\B 19
144
117
\\Expl{ To extract the kth digit value we can use div and mod operations.
145
118
If the radix is a power of two we can use bit-wise operations
146
119
(right shift and bit-wise and) instead.
@@ -150,16 +123,9 @@ for each num in A in reverse order
150
123
highlighted, and the digit value 0-3 (maybe the latter can be done
151
124
by just highlighting B[digit] instead).
152
125
\\Note}
153
- B[digit] = B[digit]-1
126
+ B[digit] = B[digit]-1 \\B 18
154
127
C[B[digit]] = num \\B 9
155
128
\\In}
156
129
\\Code}
157
130
158
- \\Code{
159
- PopFor
160
- \\Note{ Skip this?
161
- \\Note}
162
- for j <- n-1 downto 0 \\B 8
163
- \\Code}
164
-
165
131
` ) ;
0 commit comments