Skip to content

Commit 364d5f9

Browse files
committed
Decimal + base 4 display for radix
1 parent 09ee9dc commit 364d5f9

File tree

5 files changed

+78
-37
lines changed

5 files changed

+78
-37
lines changed

src/algorithms/controllers/straightRadixSort.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ export default {
272272

273273
chunker.add(SRS_BOOKMARKS.radix_sort,
274274
(vis, array) => {
275+
vis.mask.setAddBase4(true); // add Base 4 display
275276
setArray(vis.array, array);
276277

277278
if (isCountExpanded()) {
Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,58 @@
1-
# Most Significant Digit / Bit Radix Exchange Sort
1+
# Most Significant Digit Radix Sort
22

33
---
44

5-
## Introduction to Most Significant Digit / Bit Radix Exchange Sort (MSDRexSort)
6-
The Most Significant Digit/Bit (MSD/MSB) variant of Radix Sort is a recursive algorithm that sorts an array (of integers)
7-
by focusing on individual bits, starting from the most significant (left most) bit and working down to the least significant
8-
one as indicated by the binary representation and mask. <br>
5+
## Introduction
6+
Unlike most sorting algorithms, which are based on *comparison* of whole
7+
keys, radix sorting is based on examining individual "digits" in the
8+
keys.
9+
Most Significant Digit (MSD) Radix Sort (also known as Radix Exchange Sort)
10+
is a recursive algorithm that sorts an array of keys (here we use integers)
11+
by focusing on individual bits, starting from the most significant (left
12+
most) bit of each key and working down to the least significant bit.
913

10-
By sorting based on the most significant bits first, it ensures that larger or more important bits are considered before
11-
smaller ones.
12-
13-
## Processes of MSDRexSort
14+
## The algorithm
1415

1516
### 1. Initial Setup:
16-
* The ```Rexsort``` function sets up the sorting process by determining the largest bit position in the array element,
17-
referred to as the ```mask```. This ```mask``` serves as a reference for which bit is being compared in the following
18-
steps.
19-
* The recursive sorting function ```RexsortRecursive``` is then called to sort the entire array starting from the most significant
20-
bit.
17+
* The ```Rexsort``` function sets up the sorting process by determining the
18+
most significant bit position in the array elements.
19+
* A *mask* is created that has a 1 in this bit position and 0 elsewhere. The
20+
mask is used to extract bits from keys during sorting.
21+
* The recursive sorting function ```RexsortRecursive``` is then called to
22+
sort the entire array starting with this mask. The mask is shifted to the
23+
right for recursive calls.
2124
### 2. Recursive Sorting:
22-
* The ```RexsortRecursive``` function works on sub-arrays, defined by a left and right index/guard, and recursively divides
23-
the array based on the current bit position determined by the ```mask```.
24-
* If the segment contains more than one element, ```i < j``` and the mask is greater than 0, the function proceeds to partition
25-
the array.
25+
* The ```RexsortRecursive``` function works on sub-arrays, defined by a left
26+
and right index, and additionally has the current mask as a parameter.
27+
* If the sub-array contains no more than one element or the mask is 0, the
28+
function simply returns.
29+
* Otherwise, the array is first *partitioned* into two sub-arrays using the
30+
mask bit. The
31+
(smaller) keys with a 0 mask bit are put at the left and (larger) keys with a 1
32+
mask bit are put at the right (similar to partition in quicksort).
33+
* Finally, the two sub-arrays are recursively sorted, using the appropriate
34+
indices for the sub-arrays boundaries and and the mask shifted one bit
35+
position to the right.
2636

2737
### 3. Partitioning Based on Current Bit
28-
* The guards ```i``` and ```j```, are set at the left and right of the array segment.
29-
* These guards move towards each other, adjusting positions based on the bit value at the current ```mask``` position
30-
i.e.,
31-
* Increment ```i``` until an element with a 1 in the current ```mask``` position is found.
32-
* Decrement ```j``` until an element with a 0 in the current ```mask``` position is found.
33-
34-
* If the two guards have not crossed, perform a swap. to ensure that 0s and placed on the left and 1s on the right.
38+
* The indices ```i``` and ```j```, are set at the left and right of the array segment.
39+
* These indices move towards each other, adjusting positions based on the
40+
mask bit of each key:
41+
* Increment ```i``` until an element with a 1 as the mask bit is reached
42+
* Decrement ```j``` until an element with a 0 as the mask bit is reached
3543

36-
### 4. Completion
37-
Once the array is done with sorting its left and right sub arrays, the ```mask``` is reduced by 1 to shift the focus to
38-
the next bit. The recursion continues until the entire array is sorted based on all bits, from the most significant bit to
39-
the least significant bit.
44+
* If the two guards have not crossed, perform a swap to ensure that 0s are placed on the left and 1s on the right.
4045

4146
## Complexity
4247
### Time Complexity
43-
* ```n``` is the number elements in the array
44-
* ```log(largest bit)``` is the number of bits required to represent the largest number in the array
45-
* Hence, overall time complexity is ```O(n * log(largest bit)```
48+
* Partitioning takes```O(n)```, where ```n``` is the number elements in the
49+
array.
50+
* If we *assume the number of bits is limited* the overall time complexity is: ```O(n)```
51+
* Note that the best comparison-based sorting algorithms have ```O(n log n)```
52+
complexity, but the number of bits is typically related to ```log n```
53+
so radix sorts are not necessarily better.
54+
4655
### Space Complexity
47-
```O(log(largest bit))```
56+
Extra space is required for the stack but, unlike quicksort, the stack depth
57+
is limited by the number of bits, so it is a constant (if the number of bits
58+
is limited).

src/components/DataStructures/Mask/BinaryRenderer/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import React, { useCallback } from 'react';
22
import styles from './BinaryRenderer.module.scss';
33
import PropTypes from 'prop-types';
44

5-
const BinaryRenderer = ({ header, data, maxBits, highlight }) => {
5+
// renders integers as binary (by default), or other base, with optional
6+
// highlighting of some digits
7+
const BinaryRenderer = ({ header, data, maxBits, highlight, base = 2 }) => {
68
const binary = useCallback(() => {
7-
let binaryString = data.toString(2);
9+
let binaryString = data.toString(base);
810
if (binaryString.length < maxBits) {
911
binaryString = binaryString.padStart(maxBits, '0');
1012
}
@@ -38,6 +40,7 @@ BinaryRenderer.propTypes = ({
3840
data: PropTypes.number.isRequired,
3941
maxBits: PropTypes.number,
4042
highlight: PropTypes.array,
43+
base: PropTypes.number,
4144
});
4245

4346
export default BinaryRenderer;

src/components/DataStructures/Mask/MaskRenderer/index.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,33 @@ class MaskRenderer extends Renderer {
1414
this.toggleZoom(true);
1515
}
1616

17+
// display key in decimal, base 4 (optional) and binary plus
18+
// mask in binary; some (non-decimal) digits highlighted
1719
renderData() {
18-
const { binaryData, maskData, maxBits, highlight } = this.props.data;
20+
const { binaryData, maskData, maxBits, highlight, addBase4 } = this.props.data;
21+
let extra = <div/>;
22+
if (addBase4) {
23+
console.log([highlight,highlight.map((b) => Math.trunc(parseInt(b)/2))]);
24+
extra =
25+
<BinaryRenderer
26+
header={"Base 4"}
27+
data={binaryData}
28+
maxBits={maxBits/2}
29+
highlight={highlight.map((b) => Math.trunc(parseInt(b)/2))}
30+
base={4}
31+
/>
32+
}
1933
return (
2034
<div className={styles.container}>
2135
<BinaryRenderer
22-
header={"Binary Rep"}
36+
header={"Decimal"}
37+
data={binaryData}
38+
highlight={[]}
39+
base={10}
40+
/>
41+
{extra}
42+
<BinaryRenderer
43+
header={"Binary"}
2344
data={binaryData}
2445
maxBits={maxBits}
2546
highlight={highlight}

src/components/DataStructures/Mask/MaskTracer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class MaskTracer extends Tracer {
1313
this.binaryData = 0;
1414
this.maskData = 0;
1515
this.highlight = [];
16+
this.addBase4 = false;
17+
}
18+
19+
setAddBase4(b) {
20+
this.addBase4 = b;
1621
}
1722

1823
setMaxBits(bits) {

0 commit comments

Comments
 (0)