@@ -8,9 +8,9 @@ NOTE that j can start off the RHS of the array.
8
8
9
9
\\Code{
10
10
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
12
12
\\In{
13
- mask <- maximum bit used
13
+ mask <- maximum bit used \\B 100
14
14
\\Expl{ mask is a power of two (a bit string with a single "1" in it). We
15
15
start with the mask bit being at least as big as any bit that is "1" in
16
16
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.
19
19
\\Expl}
20
20
\\Note{ implementation should scan data
21
21
\\Note}
22
- Rexsort1 (A, 1, n, mask)
22
+ RexsortRecursive (A, 1, n, mask) \\B 200
23
23
\\Expl{ We need left and right indices because the code is recursive
24
24
and both may be different for recursive calls. For each call, all
25
25
elements in the array segment must have the same pattern
26
26
of bits for all bits larger than the mask bit.
27
27
\\Expl}
28
28
\\In}
29
29
//======================================================================
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
31
31
\\Expl{
32
32
Only the mask bit and smaller bits are used for sorting; higher bits
33
33
should be the same for all data in the array segment.
34
34
\\Expl}
35
- if (left < right and mask > 0) \\B 2
35
+ if (left < right and mask > 0) \\B 300
36
36
\\Expl{ Terminating condition (if there are less than two
37
37
elements in the array segment or no bits left, do nothing).
38
38
\\Expl}
39
39
\\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.
42
42
We start with an unordered array segment, and finish
43
43
with an array segment containing elements with 0 as the
44
44
mask bit at the left and 1 as the mask bit at the right.
45
45
Sets i to the index of the first "1" element (or
46
46
right+1 if there are none).
47
47
\\Expl}
48
- Sort FirstPart \\Ref RexsortFirst
48
+ Sort Left Part \\Ref MSDRadixSortLeft
49
49
\\Expl{ Sort elements with 0 mask bit: A[left]..A[i-1]
50
50
\\Expl}
51
- Sort SecondPart \\Ref RexsortSecond
51
+ Sort Right Part \\Ref MSDRadixSortRight
52
52
\\Expl{ Sort elements with 1 mask bit: A[i]..A[right]
53
53
\\Expl}
54
54
\\In}
55
- // Done \\B 19
55
+ // Done \\B 5000
56
56
\\Code}
57
57
58
58
\\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
62
62
\\Code}
63
63
64
64
\\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
68
68
\\Code}
69
69
70
70
\\Code{
71
71
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
73
73
\\Expl{ i scans from left to right stopping at "large" elements
74
74
(with "1" as the mask bit) and j scans from right to left
75
75
stopping at "small" elements (with "0" as the mask bit).
76
76
\\Expl}
77
- while i < j \\B 6
77
+ while i < j \\B 303
78
78
\\Expl{ When the indices cross, all the large elements at the left of
79
79
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
81
81
if we use "break" or similar to exit from this loop.
82
82
\\Expl}
83
83
\\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
85
85
\\Expl{ Scan right looking for a "large" element that is out of
86
86
place. Bitwise "and" between A[i] and mask can be used to
87
87
extract the desired bit.
88
88
\\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
90
90
\\Expl{ Scan left looking for a "small" element that is out of
91
91
place. Bitwise "and" between A[i] and mask can be used to
92
92
extract the desired bit.
93
93
\\Expl}
94
- if j > i \\B 9
94
+ if j > i \\B 309
95
95
\\Expl{ If the indices cross, we exit the loop.
96
96
\\Expl}
97
97
\\In{
98
- swap(A[i], A[j]) \\B 10
98
+ swap(A[i], A[j]) \\B 310
99
99
\\Expl{ Swap the larger element (A[i]) with the smaller
100
100
element (A[j]).
101
101
\\Expl}
@@ -104,16 +104,12 @@ while i < j \\B 6
104
104
\\Code}
105
105
106
106
\\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
112
110
\\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
117
113
\\Expl}
118
114
\\Code}
119
115
0 commit comments