Skip to content

Commit 33bdfa4

Browse files
committed
MSDRadixSort pseudocode draft added
1 parent a8f7ac6 commit 33bdfa4

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import parse from '../../pseudocode/parse';
2+
3+
export default parse(`
4+
\\Note{ REAL specification of radix exchange sort
5+
XXX modifying quicksort code
6+
\\Note}
7+
8+
\\Code{
9+
Main
10+
Rexsort(A, n) // Sort array A[1]..A[n] in ascending order.
11+
\\In{
12+
mask <- maximum bit used
13+
\\Expl{ mask is a power of two (a bit string with a single "1" in it). We
14+
start with the mask bit being at least as big as any bit that is "1" in
15+
the data. This can be determined from the word size used to represent
16+
the data or by scanning through the data (we do the latter here
17+
because only small examples are used).
18+
\\Expl}
19+
Rexsort1(A, 1, n, mask)
20+
\\Expl{ We need left and right indices because the code is recursive
21+
and both may be different for recursive calls. For each call, all
22+
elements in the array segment are guaranteed to have the same pattern
23+
of bits for all bits larger than the mask bit.
24+
\\Expl}
25+
\\In}
26+
//======================================================================
27+
Rexsort1(A, left, right, mask) // Sort array A[left]..A[right] using bits up to mask \\B 1
28+
\\Expl}
29+
if (left < right and mask > 0) \\B 2
30+
\\Expl{ Terminating condition (if there are less than two
31+
elements in the array segment or no bits left, do nothing).
32+
\\Expl}
33+
\\In{
34+
Partition array segment using mask \\Ref Partition
35+
\\Expl{ This is where most of the work of Rexsort gets done.
36+
We start with an unordered array segment, and finish
37+
with an array segment containing elements with 0 as the
38+
mask bit at the left and 1 as the mask bit at the right.
39+
The index of the first "1" element is returned.
40+
\\Expl}
41+
Sort FirstPart \\Ref RexsortFirst
42+
\\Expl{ Sort elements with 0 mask bit: A[left]..A[i-1]
43+
\\Expl}
44+
Sort SecondPart \\Ref RexsortSecond
45+
\\Expl{ Sort elements with 1 mask bit: A[i]..A[right]
46+
\\Expl}
47+
\\In}
48+
// Done \\B 19
49+
\\Code}
50+
51+
\\Code{
52+
RexsortFirst
53+
// *Recursively* sort first part: \\B 300
54+
Rexsort1(A, left, i - 1) \\B 3
55+
\\Code}
56+
57+
\\Code{
58+
RexsortSecond
59+
// *Recursively* sort second part: \\B 400
60+
Rexsort1(A, i, right) \\B 4
61+
\\Code}
62+
63+
\\Code{
64+
Partition
65+
Set index i at left the of array segment and j at the right \\Ref init_iAndj
66+
\\Expl{ i scans from left to right stopping at "large" elements
67+
(with "1" as the mask bit) and j scans from right to left
68+
stopping at "small" (with "0" as the mask bit) elements.
69+
\\Expl}
70+
while i < j \\B 6
71+
\\Expl{ When the indices cross, all the large elements at the left of
72+
the array segment have been swapped with small elements from the
73+
right of the array segment. The coding here can be simplified
74+
if we use "break" or similar to exit from this loop.
75+
\\Expl}
76+
\\In{
77+
Repeatedly increment i until i >= j or A[i] has 1 as the mask bit \\B 7
78+
\\Expl{ XXX
79+
\\Expl}
80+
Repeatedly decrement j until j <= i or A[j] has 0 as the mask bit \\B 8
81+
\\Expl{ XXX
82+
\\Expl}
83+
if j > i \\B 9
84+
\\Expl{ If the indices cross, we exit the loop.
85+
\\Expl}
86+
\\In{
87+
swap(A[i], A[j]) \\B 10
88+
\\Expl{ Swap the larger element (A[i]) with the smaller
89+
element (A[j]).
90+
\\Expl}
91+
\\In}
92+
\\In}
93+
// Put the pivot in its final place
94+
swap(A[i], A[right]) \\B 13
95+
\\Expl{ The pivot element, in A[right], is swapped with A[i]. All
96+
elements to the left of A[i] must be less than or equal to
97+
the pivot and A[i] plus all elements to its right must be
98+
greater than or equal to the pivot, thus the pivot is now in its
99+
final position and is not considered further.
100+
\\Expl}
101+
\\Code}
102+
103+
\\Code{
104+
init_iAndj
105+
i <- left - 1 \\B 11
106+
\\Expl{ The i pointer scans left to right with a preincrement, so
107+
it is set to left - 1 (this may be off the left end of the array but
108+
we never access that element).
109+
\\Expl}
110+
j <- right + 1 \\B 12
111+
\\Expl{ The j pointer scans right to left with a predecrement and
112+
is set to right + 1 (this may be off the left end of the array but
113+
we never access that element).
114+
\\Expl}
115+
\\Code}
116+
117+
`);

0 commit comments

Comments
 (0)