Skip to content

Commit 5534a01

Browse files
committed
refactor: add MSD Radix Sort pseudocode
1 parent 694d65f commit 5534a01

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

src/algorithms/pseudocode/MSDRadixSort.js

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ NOTE that j can start off the RHS of the array.
88
99
\\Code{
1010
Main
11-
Rexsort(A, n) // Sort array A[1]..A[n] in ascending order.
11+
Rexsort(A, n) // Sort array A[1]..A[n] in ascending order. \\B 1
1212
\\In{
13-
mask <- maximum bit used
13+
mask <- maximum bit used \\B 100
1414
\\Expl{ mask is a power of two (a bit string with a single "1" in it). We
1515
start with the mask bit being at least as big as any bit that is "1" in
1616
the data. This can be determined from the word size used to represent
@@ -19,83 +19,83 @@ Rexsort(A, n) // Sort array A[1]..A[n] in ascending order.
1919
\\Expl}
2020
\\Note{ implementation should scan data
2121
\\Note}
22-
Rexsort1(A, 1, n, mask)
22+
RexsortRecursive(A, 1, n, mask) \\B 200
2323
\\Expl{ We need left and right indices because the code is recursive
2424
and both may be different for recursive calls. For each call, all
2525
elements in the array segment must have the same pattern
2626
of bits for all bits larger than the mask bit.
2727
\\Expl}
2828
\\In}
2929
//======================================================================
30-
Rexsort1(A, left, right, mask) // Sort array A[left]..A[right] using bits up to mask \\B 1
30+
RexsortRecursive(A, left, right, mask) // Sort array A[left]..A[right] using bits up to mask
3131
\\Expl{
3232
Only the mask bit and smaller bits are used for sorting; higher bits
3333
should be the same for all data in the array segment.
3434
\\Expl}
35-
if (left < right and mask > 0) \\B 2
35+
if (left < right and mask > 0) \\B 300
3636
\\Expl{ Terminating condition (if there are less than two
3737
elements in the array segment or no bits left, do nothing).
3838
\\Expl}
3939
\\In{
40-
Partition array segment using mask \\Ref Partition
41-
\\Expl{ This is where most of the work of Rexsort gets done.
40+
Partition array segment using mask \\Ref Partition
41+
\\Expl{ This is where most of the work of MSDRadixSort gets done.
4242
We start with an unordered array segment, and finish
4343
with an array segment containing elements with 0 as the
4444
mask bit at the left and 1 as the mask bit at the right.
4545
Sets i to the index of the first "1" element (or
4646
right+1 if there are none).
4747
\\Expl}
48-
Sort FirstPart \\Ref RexsortFirst
48+
Sort Left Part \\Ref MSDRadixSortLeft
4949
\\Expl{ Sort elements with 0 mask bit: A[left]..A[i-1]
5050
\\Expl}
51-
Sort SecondPart \\Ref RexsortSecond
51+
Sort Right Part \\Ref MSDRadixSortRight
5252
\\Expl{ Sort elements with 1 mask bit: A[i]..A[right]
5353
\\Expl}
5454
\\In}
55-
// Done \\B 19
55+
// Done \\B 5000
5656
\\Code}
5757
5858
\\Code{
59-
RexsortFirst
60-
// *Recursively* sort first part: \\B 300
61-
Rexsort1(A, left, i - 1) \\B 3
59+
MSDRadixSortLeft
60+
// *Recursively* sort first part: \\B 400
61+
RexsortRecursive(A, left, i-1, mask-1) \\B 401
6262
\\Code}
6363
6464
\\Code{
65-
RexsortSecond
66-
// *Recursively* sort second part: \\B 400
67-
Rexsort1(A, i, right) \\B 4
65+
MSDRadixSortRight
66+
// *Recursively* sort first part: \\B 500
67+
RexsortRecursive(A, i, right, mask-1) \\B 501
6868
\\Code}
6969
7070
\\Code{
7171
Partition
72-
Set index i at left the of array segment and j at the right \\Ref init_iAndj
72+
Set index i at left the of array segment and j at the right \\Ref InitCounters
7373
\\Expl{ i scans from left to right stopping at "large" elements
7474
(with "1" as the mask bit) and j scans from right to left
7575
stopping at "small" elements (with "0" as the mask bit).
7676
\\Expl}
77-
while i < j \\B 6
77+
while i < j \\B 303
7878
\\Expl{ When the indices cross, all the large elements at the left of
7979
the array segment have been swapped with small elements from the
80-
right of the array segment. The coding here can be simplified
80+
right of the array segment. The coding here can be simplified
8181
if we use "break" or similar to exit from this loop.
8282
\\Expl}
8383
\\In{
84-
Repeatedly increment i until i >= j or A[i] has 1 as the mask bit \\B 7
84+
Repeatedly increment i until i >= j or A[i] has 1 as the mask bit \\B 304
8585
\\Expl{ Scan right looking for a "large" element that is out of
8686
place. Bitwise "and" between A[i] and mask can be used to
8787
extract the desired bit.
8888
\\Expl}
89-
Repeatedly decrement j until j <= i or A[j] has 0 as the mask bit \\B 8
89+
Repeatedly decrement j until j <= i or A[j] has 0 as the mask bit \\B 305
9090
\\Expl{ Scan left looking for a "small" element that is out of
9191
place. Bitwise "and" between A[i] and mask can be used to
9292
extract the desired bit.
9393
\\Expl}
94-
if j > i \\B 9
94+
if j > i \\B 309
9595
\\Expl{ If the indices cross, we exit the loop.
9696
\\Expl}
9797
\\In{
98-
swap(A[i], A[j]) \\B 10
98+
swap(A[i], A[j]) \\B 310
9999
\\Expl{ Swap the larger element (A[i]) with the smaller
100100
element (A[j]).
101101
\\Expl}
@@ -104,16 +104,12 @@ while i < j \\B 6
104104
\\Code}
105105
106106
\\Code{
107-
init_iAndj
108-
i <- left - 1 \\B 11
109-
\\Expl{ The i pointer scans left to right with a preincrement, so
110-
it is set to left - 1 (this may be off the left end of the array but
111-
we never access that element).
107+
InitCounters
108+
i <- left \\B 301
109+
\\Expl{ The i pointer scans left to right, so it is set to left
112110
\\Expl}
113-
j <- right + 1 \\B 12
114-
\\Expl{ The j pointer scans right to left with a predecrement and
115-
is set to right + 1 (this may be off the right end of the array but
116-
we never access that element).
111+
j <- right \\B 302
112+
\\Expl{ The j pointer scans right to left, so it is set to right
117113
\\Expl}
118114
\\Code}
119115

src/algorithms/pseudocode/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ 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';
67
export { default as msort_lista_td } from './msort_lista_td';
78
export { default as transitiveClosure } from './transitiveClosure';
89
export { default as prim_old } from './prim_old';

0 commit comments

Comments
 (0)