Skip to content

Commit a328b98

Browse files
committed
refactor: add SRS pseudocode
1 parent 2e9dd18 commit a328b98

File tree

2 files changed

+19
-51
lines changed

2 files changed

+19
-51
lines changed

src/algorithms/pseudocode/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ export { default as binaryTreeInsertion } from './binaryTreeInsertion';
33
export { default as heapSort } from './heapSort';
44
export { default as quickSort } from './quickSort';
55
export { default as msort_arr_td } from './msort_arr_td';
6+
export { default as MSDRadixSort } from './MSDRadixSort';
7+
export { default as straightRadixSort } from './straightRadixSort';
68
export { default as msort_lista_td } from './msort_lista_td';
79
export { default as transitiveClosure } from './transitiveClosure';
810
export { default as prim_old } from './prim_old';

src/algorithms/pseudocode/straightRadixSort.js

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ would be good for the counts array.
2222
Main
2323
Radixsort(A, n) // Sort array A[1]..A[n] in ascending order. \\B 1
2424
25-
Find maximum number of "digits" used in the data
25+
Find maximum number of "digits" used in the data \\B 2
2626
\\Expl{ This depends on the radix (base) we use to view the data.
2727
We could use radix 10 (decimal digits), radix 2
2828
(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
3131
word size rather than scanning all the input data as we do here.
3232
\\Expl}
3333
34-
for each digit k up to maximum digit number
34+
for each digit k up to maximum digit number \\B 3
3535
\\Expl{ We scan the digits right to left, from least significant to
3636
most significant.
3737
\\Expl}
@@ -46,31 +46,11 @@ Radixsort(A, n) // Sort array A[1]..A[n] in ascending order. \\B 1
4646
// Done \\B 11
4747
\\Code}
4848
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-
6949
\\Code{
7050
Countingsort
7151
// 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
7454
\\Expl{ We count the number of occurrences of each digit value (0-3
7555
here) in the kth digits of the data.
7656
\\Expl}
@@ -79,11 +59,11 @@ Cumulatively sum digit value counts \\Ref CumSum
7959
plus all smaller digit values. This allows us to determine where the
8060
last occurrence of each digit value will appear in the sorted array.
8161
\\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
8464
value counts to determine where each element is copied to.
8565
\\Expl}
86-
Copy C back to A \\B 10
66+
Copy B back to A \\B 10
8767
\\Expl{ Array A is now sorted on digit k and all smaller digits
8868
(because the smaller digits were sorted previously and counting
8969
sort is stable).
@@ -92,11 +72,11 @@ Copy C back to A \\B 10
9272
9373
\\Code{
9474
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
9878
\\In{
99-
digit <- kth digit value in num
79+
digit <- kth digit value in num \\B 17
10080
\\Expl{ To extract the kth digit we can use div and mod operations.
10181
If the radix is a power of two we can use bit-wise operations
10282
(right shift and bit-wise and) instead.
@@ -106,41 +86,34 @@ for num in A
10686
highlighted, and the digit value 0-3 (maybe the latter can be done
10787
by just highlighting B[digit] instead).
10888
\\Note}
109-
B[digit] <- B[digit]+1
89+
C[digit] <- C[digit]+1 \\B 12
11090
\\In}
11191
\\Code}
11292
113-
\\Code{
114-
KthBit
115-
\\Note{ Skip this
116-
\\Note}
117-
bit <- (num & (1 << i)) >> i
118-
\\Code}
119-
12093
\\Code{
12194
CumSum
12295
// Cumulatively sum counts \\B 6
12396
\\Note{ Best remove this comment line and move bookmark
12497
\\Note}
125-
for i = 1 to maximum digit value
98+
for i = 1 to maximum digit value \\B 14
12699
\\Expl{ We must scan left to right. The count for digit 0 remains
127100
unchanged.
128101
\\Expl}
129102
\\In{
130-
B[i] = B[i-1] + B[i]
103+
B[i] = B[i-1] + B[i] \\B 15
131104
\\In}
132105
\\Code}
133106
134107
\\Code{
135108
Populate
136109
// 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
138111
\\Expl{ We go from right to left so that we preserve the order of numbers
139112
with the same digit.
140113
This is CRUCIAL in radix sort as the counting sort MUST be stable.
141114
\\Expl}
142115
\\In{
143-
digit <- kth digit value in num \\Ref KthBit
116+
digit <- kth digit value in num \\B 19
144117
\\Expl{ To extract the kth digit value we can use div and mod operations.
145118
If the radix is a power of two we can use bit-wise operations
146119
(right shift and bit-wise and) instead.
@@ -150,16 +123,9 @@ for each num in A in reverse order
150123
highlighted, and the digit value 0-3 (maybe the latter can be done
151124
by just highlighting B[digit] instead).
152125
\\Note}
153-
B[digit] = B[digit]-1
126+
B[digit] = B[digit]-1 \\B 18
154127
C[B[digit]] = num \\B 9
155128
\\In}
156129
\\Code}
157130
158-
\\Code{
159-
PopFor
160-
\\Note{ Skip this?
161-
\\Note}
162-
for j <- n-1 downto 0 \\B 8
163-
\\Code}
164-
165131
`);

0 commit comments

Comments
 (0)