Skip to content

Commit e3684d6

Browse files
committed
MSDRadixSort pseudocode done
1 parent 33bdfa4 commit e3684d6

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

src/algorithms/pseudocode/MSDRadixSort.js

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import parse from '../../pseudocode/parse';
22

33
export default parse(`
44
\\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.
67
\\Note}
78
89
\\Code{
@@ -16,18 +17,23 @@ Rexsort(A, n) // Sort array A[1]..A[n] in ascending order.
1617
the data or by scanning through the data (we do the latter here
1718
because only small examples are used).
1819
\\Expl}
20+
\\Note{ implementation should scan data
21+
\\Note}
1922
Rexsort1(A, 1, n, mask)
2023
\\Expl{ We need left and right indices because the code is recursive
2124
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
2326
of bits for all bits larger than the mask bit.
2427
\\Expl}
2528
\\In}
2629
//======================================================================
2730
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.
2834
\\Expl}
2935
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
3137
elements in the array segment or no bits left, do nothing).
3238
\\Expl}
3339
\\In{
@@ -36,13 +42,14 @@ Rexsort1(A, left, right, mask) // Sort array A[left]..A[right] using bits up to
3642
We start with an unordered array segment, and finish
3743
with an array segment containing elements with 0 as the
3844
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).
4047
\\Expl}
4148
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]
4350
\\Expl}
4451
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]
4653
\\Expl}
4754
\\In}
4855
// Done \\B 19
@@ -63,53 +70,49 @@ Rexsort1(A, i, right) \\B 4
6370
\\Code{
6471
Partition
6572
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
6774
(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).
6976
\\Expl}
7077
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
7279
the array segment have been swapped with small elements from the
7380
right of the array segment. The coding here can be simplified
7481
if we use "break" or similar to exit from this loop.
7582
\\Expl}
7683
\\In{
7784
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.
7988
\\Expl}
8089
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.
8293
\\Expl}
8394
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.
8596
\\Expl}
8697
\\In{
8798
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
89100
element (A[j]).
90101
\\Expl}
91102
\\In}
92103
\\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}
101104
\\Code}
102105
103106
\\Code{
104107
init_iAndj
105108
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
107110
it is set to left - 1 (this may be off the left end of the array but
108111
we never access that element).
109112
\\Expl}
110113
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
113116
we never access that element).
114117
\\Expl}
115118
\\Code}

src/algorithms/pseudocode/quickSort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Partition
7777
Set index i at left the of array segment and j at the right \\Ref init_iAndj
7878
\\Expl{ i scans from left to right stopping at "large" elements
7979
(greater than or equal to the pivot) and j scans from right to left
80-
stopping at "small" (less than or equal to the pivot) elements.
80+
stopping at "small" elements (less than or equal to the pivot).
8181
\\Expl}
8282
while i < j \\B 6
8383
\\Expl{ When the indices cross, all the large elements at the left of

src/algorithms/pseudocode/quickSortM3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Partition
9595
Set index i at left the of array segment and j at the right \\Ref init_iAndj
9696
\\Expl{ i scans from left to right stopping at "large" elements
9797
(greater than or equal to the pivot) and j scans from right to left
98-
stopping at "small" (less than or equal to the pivot) elements.
98+
stopping at "small" elements (less than or equal to the pivot).
9999
\\Expl}
100100
while i < j \\B 6
101101
\\Expl{ When the indices cross, all the large elements at the left of

0 commit comments

Comments
 (0)