@@ -2,7 +2,8 @@ import parse from '../../pseudocode/parse';
2
2
3
3
export default parse ( `
4
4
\\Note{ REAL specification of radix exchange sort
5
- XXX modifying quicksort code
5
+ Modified quicksort code: will need extra bookmarks for top level plus
6
+ NOTE that j can start off the RHS of the array.
6
7
\\Note}
7
8
8
9
\\Code{
@@ -16,18 +17,23 @@ Rexsort(A, n) // Sort array A[1]..A[n] in ascending order.
16
17
the data or by scanning through the data (we do the latter here
17
18
because only small examples are used).
18
19
\\Expl}
20
+ \\Note{ implementation should scan data
21
+ \\Note}
19
22
Rexsort1(A, 1, n, mask)
20
23
\\Expl{ We need left and right indices because the code is recursive
21
24
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
25
+ elements in the array segment must have the same pattern
23
26
of bits for all bits larger than the mask bit.
24
27
\\Expl}
25
28
\\In}
26
29
//======================================================================
27
30
Rexsort1(A, left, right, mask) // Sort array A[left]..A[right] using bits up to mask \\B 1
31
+ \\Expl{
32
+ Only the mask bit and smaller bits are used for sorting; higher bits
33
+ should be the same for all data in the array segment.
28
34
\\Expl}
29
35
if (left < right and mask > 0) \\B 2
30
- \\Expl{ Terminating condition (if there are less than two
36
+ \\Expl{ Terminating condition (if there are less than two
31
37
elements in the array segment or no bits left, do nothing).
32
38
\\Expl}
33
39
\\In{
@@ -36,13 +42,14 @@ Rexsort1(A, left, right, mask) // Sort array A[left]..A[right] using bits up to
36
42
We start with an unordered array segment, and finish
37
43
with an array segment containing elements with 0 as the
38
44
mask bit at the left and 1 as the mask bit at the right.
39
- The index of the first "1" element is returned.
45
+ Sets i to the index of the first "1" element (or
46
+ right+1 if there are none).
40
47
\\Expl}
41
48
Sort FirstPart \\Ref RexsortFirst
42
- \\Expl{ Sort elements with 0 mask bit: A[left]..A[i-1]
49
+ \\Expl{ Sort elements with 0 mask bit: A[left]..A[i-1]
43
50
\\Expl}
44
51
Sort SecondPart \\Ref RexsortSecond
45
- \\Expl{ Sort elements with 1 mask bit: A[i]..A[right]
52
+ \\Expl{ Sort elements with 1 mask bit: A[i]..A[right]
46
53
\\Expl}
47
54
\\In}
48
55
// Done \\B 19
@@ -63,53 +70,49 @@ Rexsort1(A, i, right) \\B 4
63
70
\\Code{
64
71
Partition
65
72
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
73
+ \\Expl{ i scans from left to right stopping at "large" elements
67
74
(with "1" as the mask bit) and j scans from right to left
68
- stopping at "small" (with "0" as the mask bit) elements .
75
+ stopping at "small" elements (with "0" as the mask bit).
69
76
\\Expl}
70
77
while i < j \\B 6
71
- \\Expl{ When the indices cross, all the large elements at the left of
78
+ \\Expl{ When the indices cross, all the large elements at the left of
72
79
the array segment have been swapped with small elements from the
73
80
right of the array segment. The coding here can be simplified
74
81
if we use "break" or similar to exit from this loop.
75
82
\\Expl}
76
83
\\In{
77
84
Repeatedly increment i until i >= j or A[i] has 1 as the mask bit \\B 7
78
- \\Expl{ XXX
85
+ \\Expl{ Scan right looking for a "large" element that is out of
86
+ place. Bitwise "and" between A[i] and mask can be used to
87
+ extract the desired bit.
79
88
\\Expl}
80
89
Repeatedly decrement j until j <= i or A[j] has 0 as the mask bit \\B 8
81
- \\Expl{ XXX
90
+ \\Expl{ Scan left looking for a "small" element that is out of
91
+ place. Bitwise "and" between A[i] and mask can be used to
92
+ extract the desired bit.
82
93
\\Expl}
83
94
if j > i \\B 9
84
- \\Expl{ If the indices cross, we exit the loop.
95
+ \\Expl{ If the indices cross, we exit the loop.
85
96
\\Expl}
86
97
\\In{
87
98
swap(A[i], A[j]) \\B 10
88
- \\Expl{ Swap the larger element (A[i]) with the smaller
99
+ \\Expl{ Swap the larger element (A[i]) with the smaller
89
100
element (A[j]).
90
101
\\Expl}
91
102
\\In}
92
103
\\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
104
\\Code}
102
105
103
106
\\Code{
104
107
init_iAndj
105
108
i <- left - 1 \\B 11
106
- \\Expl{ The i pointer scans left to right with a preincrement, so
109
+ \\Expl{ The i pointer scans left to right with a preincrement, so
107
110
it is set to left - 1 (this may be off the left end of the array but
108
111
we never access that element).
109
112
\\Expl}
110
113
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
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
113
116
we never access that element).
114
117
\\Expl}
115
118
\\Code}
0 commit comments