|
| 1 | +import parse from '../../pseudocode/parse'; |
| 2 | + |
| 3 | +export default parse(` |
| 4 | +\\Note{ REAL specification of straight radix sort |
| 5 | +
|
| 6 | +
|
| 7 | +
|
| 8 | +XXXXXXXXXXXXXXXXXXXX Notes from Lee XXXXXXXXXXXXXXXXXXXXXXXXX |
| 9 | +
|
| 10 | +I've had a pass over this. A few details I've removed one bit added plus |
| 11 | +changed "bits" to digits - best use radix 4 for now at least (though |
| 12 | +writing the code so we can change the radix very easily would be a |
| 13 | +good idea). See also Notes in the code. I've not changed the bookmarks |
| 14 | +so (hopefully) I've not broken anything in the current animation code. |
| 15 | +
|
| 16 | +Also (not done here). Best rename the temporary data and count |
| 17 | +arrays. Array B for the temporary array would be fine. Array C or Counts |
| 18 | +would be good for the counts array. |
| 19 | +\\Note} |
| 20 | +
|
| 21 | +\\Code{ |
| 22 | +Main |
| 23 | +Radixsort(A, n) // Sort array A[1]..A[n] in ascending order. \\B 1 |
| 24 | +
|
| 25 | + Find maximum number of "digits" used in the data |
| 26 | + \\Expl{ This depends on the radix (base) we use to view the data. |
| 27 | + We could use radix 10 (decimal digits), radix 2 |
| 28 | + (binary) or anything else. Here we use radix 4 for illustration |
| 29 | + (the digits are 0-3 and use two bits). Radix 256 (one byte) is a |
| 30 | + better choice in practice and we can set the maximum to be the |
| 31 | + word size rather than scanning all the input data as we do here. |
| 32 | + \\Expl} |
| 33 | +
|
| 34 | + for each digit k up to maximum digit number |
| 35 | + \\Expl{ We scan the digits right to left, from least significant to |
| 36 | + most significant. |
| 37 | + \\Expl} |
| 38 | + \\In{ |
| 39 | + Countingsort(A, k, n) \\Ref Countingsort |
| 40 | + \\Expl{ Straight Radix Sort uses Counting Sort to stably sort the |
| 41 | + array, treating each digit value of the original data as a key in |
| 42 | + Counting Sort. |
| 43 | + \\Expl} |
| 44 | + \\In} |
| 45 | +
|
| 46 | +// Done \\B 11 |
| 47 | +\\Code} |
| 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 | +\\Code{ |
| 70 | +Countingsort |
| 71 | +// 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 |
| 74 | +\\Expl{ We count the number of occurrences of each digit value (0-3 |
| 75 | + here) in the kth digits of the data. |
| 76 | +\\Expl} |
| 77 | +Cumulatively sum digit value counts \\Ref CumSum |
| 78 | +\\Expl{ For each digit value, we compute the count for that digit value |
| 79 | + plus all smaller digit values. This allows us to determine where the |
| 80 | + last occurrence of each digit value will appear in the sorted array. |
| 81 | +\\Expl} |
| 82 | +Populate temporary array C with sorted numbers \\Ref Populate |
| 83 | +\\Expl{ We copy the data to temporary array C, using the digit |
| 84 | + value counts to determine where each element is copied to. |
| 85 | +\\Expl} |
| 86 | +Copy C back to A \\B 10 |
| 87 | +\\Expl{ Array A is now sorted on digit k and all smaller digits |
| 88 | + (because the smaller digits were sorted previously and counting |
| 89 | + sort is stable). |
| 90 | +\\Expl} |
| 91 | +\\Code} |
| 92 | +
|
| 93 | +\\Code{ |
| 94 | +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 |
| 98 | +\\In{ |
| 99 | + digit <- kth digit value in num |
| 100 | + \\Expl{ To extract the kth digit we can use div and mod operations. |
| 101 | + If the radix is a power of two we can use bit-wise operations |
| 102 | + (right shift and bit-wise and) instead. |
| 103 | + \\Expl} |
| 104 | + \\Note{ It would be nice to highlight the (decimal) number plus |
| 105 | + somewhere display it in binary with the two relevant bits |
| 106 | + highlighted, and the digit value 0-3 (maybe the latter can be done |
| 107 | + by just highlighting B[digit] instead). |
| 108 | + \\Note} |
| 109 | + B[digit] <- B[digit]+1 |
| 110 | +\\In} |
| 111 | +\\Code} |
| 112 | +
|
| 113 | +\\Code{ |
| 114 | +KthBit |
| 115 | +\\Note{ Skip this |
| 116 | +\\Note} |
| 117 | +bit <- (num & (1 << i)) >> i |
| 118 | +\\Code} |
| 119 | +
|
| 120 | +\\Code{ |
| 121 | +CumSum |
| 122 | +// Cumulatively sum counts \\B 6 |
| 123 | +\\Note{ Best remove this comment line and move bookmark |
| 124 | +\\Note} |
| 125 | +for i = 1 to maximum digit value |
| 126 | +\\Expl{ We must scan left to right. The count for digit 0 remains |
| 127 | + unchanged. |
| 128 | +\\Expl} |
| 129 | +\\In{ |
| 130 | + B[i] = B[i-1] + B[i] |
| 131 | +\\In} |
| 132 | +\\Code} |
| 133 | +
|
| 134 | +\\Code{ |
| 135 | +Populate |
| 136 | +// Populate new array C with sorted numbers \\B 7 |
| 137 | +for each num in A in reverse order |
| 138 | +\\Expl{ We go from right to left so that we preserve the order of numbers |
| 139 | + with the same digit. |
| 140 | + This is CRUCIAL in radix sort as the counting sort MUST be stable. |
| 141 | +\\Expl} |
| 142 | +\\In{ |
| 143 | + digit <- kth digit value in num \\Ref KthBit |
| 144 | + \\Expl{ To extract the kth digit value we can use div and mod operations. |
| 145 | + If the radix is a power of two we can use bit-wise operations |
| 146 | + (right shift and bit-wise and) instead. |
| 147 | + \\Expl} |
| 148 | + \\Note{ It would be nice to highlight the (decimal) number plus |
| 149 | + somewhere display it in binary with the two relevant bits |
| 150 | + highlighted, and the digit value 0-3 (maybe the latter can be done |
| 151 | + by just highlighting B[digit] instead). |
| 152 | + \\Note} |
| 153 | + B[digit] = B[digit]-1 |
| 154 | + C[B[digit]] = num \\B 9 |
| 155 | +\\In} |
| 156 | +\\Code} |
| 157 | +
|
| 158 | +\\Code{ |
| 159 | +PopFor |
| 160 | +\\Note{ Skip this? |
| 161 | +\\Note} |
| 162 | +for j <- n-1 downto 0 \\B 8 |
| 163 | +\\Code} |
| 164 | +
|
| 165 | +`); |
0 commit comments