Skip to content

Commit 7c20604

Browse files
committed
radix sort
1 parent 9c6ec60 commit 7c20604

5 files changed

Lines changed: 101 additions & 17 deletions

File tree

‎src/components/Animation/Animation.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ class Animation {
2828
/**
2929
* takes indecies to be swapped
3030
* @param {Integer} i
31-
* @param {Integer} minBarIndex
31+
* @param {Integer} k
3232
*/
33-
swap(i, minBarIndex) {
33+
static swap(i, k) {
3434
let unSortedBars = document.querySelectorAll('.array-bars');
35-
var temp1 = unSortedBars[minBarIndex].style.height;
36-
unSortedBars[minBarIndex].style.height = unSortedBars[i].style.height;
35+
var temp1 = unSortedBars[k].style.height;
36+
unSortedBars[k].style.height = unSortedBars[i].style.height;
3737
unSortedBars[i].style.height = temp1;
3838

39-
var temp2 = unSortedBars[minBarIndex].id;
40-
unSortedBars[minBarIndex].id = unSortedBars[i].id;
39+
var temp2 = unSortedBars[k].id;
40+
unSortedBars[k].id = unSortedBars[i].id;
4141
unSortedBars[i].id = temp1;
4242
}
4343

‎src/components/Canvas/InputField.js‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { Strategy } from '../Strategy/Strategy.js';
55
import { Options } from './options/Options.js';
66
import { ReactComponent as Play } from '../../img/play.svg'
77
import { ReactComponent as Replay } from '../../img/replay.svg'
8-
import { Animation } from '../Animation/Animation.js';
98
import { MergeSort } from '../Strategy/Algorithms/MergeSort.js';
9+
import { RadixSortLSD } from '../Strategy/Algorithms/RadixSortLSD.js';
1010

1111

1212
class InputField extends Component {
@@ -17,6 +17,7 @@ class InputField extends Component {
1717
this.name = props.name;
1818
this.selectionSort = new SelectionSort();
1919
this.mergeSort = new MergeSort();
20+
this.radixSortLSD = new RadixSortLSD();
2021
this.Strategy = new Strategy();
2122
this.state = {
2223
algorithm: ''
@@ -33,7 +34,7 @@ class InputField extends Component {
3334
return (
3435
<div className="flex flex-col border-[1px] bg-gray-100">
3536

36-
<div className={`flex items-center justify-center p-3 w-sreen gap-4 flex-wrap ${this.name[0] + ' '}`}>
37+
<div className={`flex items-center justify-center py-2 w-sreen gap-4 flex-wrap ${this.name[0] + ' '}`}>
3738

3839
<div className="w-[41em] overflow-auto flex gap-3 p-2">
3940
<div onClick={() => {
@@ -61,11 +62,11 @@ class InputField extends Component {
6162
</div>
6263

6364
<div onClick={() => {
64-
this.Strategy.setStrategy = this.selectionSort;
65-
this.setState({algorithm: 'Bubble'});
65+
this.Strategy.setStrategy = this.radixSortLSD;
66+
this.setState({algorithm: 'Radix'});
6667
}}
6768
className={`cursor-pointer hover:scale-[1.05] transition-all duration-200 ease-in-out border-2 hover:bg-blue-200 hover:border-blue-300 ${algorithm === 'Bubble' ? "bg-blue-100 border-blue-300" : "border-gray-300 bg-white"} min-w-[150px] rounded-xl text-gray-600 flex flex-col p-3 items-center`}>
68-
<p className="font-semibold">Bubble Sort</p>
69+
<p className="font-semibold">Radix Sort</p>
6970
</div>
7071

7172
</div>
@@ -91,19 +92,19 @@ class InputField extends Component {
9192
<div className="flex gap-10 flex-wrap justify-center">
9293
<Options options={this.props.InputHandler} algorithm={algorithm}/>
9394
<div className="flex self-center gap-3">
94-
<div onClick={() => {
95+
<div onClick={async () => {
9596
if (this.state.algorithm && inputArrayLength) {
9697
window.scroll({ // SG 07/10/2022 18:55 for mobile devices
9798
top: 1000,
9899
behavior: 'smooth'
99100
});
100101
this.Strategy.setOptions = options;
101-
this.Strategy.perform(options, inputArray)
102+
await this.Strategy.perform(options, inputArray)
102103
}
103104
}
104105
} className={`relative min-w-[3.5em] p-3 group rounded-lg flex ${this.state.algorithm && inputArrayLength ? "bg-green-600 cursor-pointer hover:shadow-custom-md-green transition-all duration-200 ease-in-out" : "bg-gray-600 cursor-not-allowed"}`}>
105-
<p className={`text-white font-semibold group-hover:opacity-0 ${this.state.algorithm && "mr-9"}`}>{this.state.algorithm && this.state.algorithm + " sort"}</p>
106-
<Play className={`absolute right-[1em] transition-all duration-200 ease-in-out ${this.state.algorithm && "group-hover:scale-[1.15] group-hover:right-[40%]"}`}/>
106+
<p className={`text-white font-semibold group-hover:opacity-0 ${this.state.algorithm && "mr-9"}`}>{this.state.algorithm && this.state.algorithm + " Sort"}</p>
107+
<Play className={`absolute right-[.94em] transition-all duration-200 ease-in-out ${this.state.algorithm && "group-hover:scale-[1.15] group-hover:right-[40%]"}`}/>
107108
</div>
108109

109110
<div className="self-center group">

‎src/components/InputHandler/InputHandler.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,20 @@ class InputHandler {
5353
static generateRandomIntArray(inputSize) {
5454
let arr = [];
5555
const MAX_BAR_HEIGHT = window.innerHeight - 280;
56+
5657
for (var i = 0; i < inputSize; i++) {
57-
arr.push(InputHandler.randomIntFromInterval(5, MAX_BAR_HEIGHT));
58+
let duplicate = false;
59+
while(!duplicate) { // SG 07/10/2022 22:04 not allowing duplicates
60+
let potentialN = InputHandler.randomIntFromInterval(5, MAX_BAR_HEIGHT);
61+
for(const num of arr) {
62+
if(potentialN === num) duplicate = true;
63+
}
64+
if(duplicate) duplicate = false;
65+
else {
66+
arr.push(potentialN);
67+
break;
68+
}
69+
}
5870
}
5971
InputHandler.setInputArray = arr;
6072
console.log(InputHandler.getGeneratedArray)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Animation } from "../../Animation/Animation";
2+
3+
4+
class RadixSortLSD {
5+
6+
getMaxValue(array) {
7+
let max = array[0];
8+
for(const num of array) {
9+
if(max < num) max = num;
10+
}
11+
12+
return max;
13+
}
14+
15+
getIndexDOM(element, DOM) {
16+
for(const e of DOM) {
17+
if(element === parseInt(e.id)) return DOM.indexOf(e);
18+
}
19+
}
20+
21+
async sort(array, exp, DOM, originalArray) {
22+
console.log("================================================================");
23+
let k = array.length;
24+
let sortedArray = new Array(k);
25+
let i = 0;
26+
let bucket = new Array(100);
27+
28+
29+
for(let i = 0; i < 100; i++) {
30+
bucket[i] = 0;
31+
}
32+
33+
for(let i = 0; i < k; i++) {
34+
bucket[Math.floor(array[i] / exp) % 100]++;
35+
}
36+
37+
for (i = 1; i < 100; i++) {
38+
bucket[i] += bucket[i - 1];
39+
}
40+
41+
// Build the sortedArray array
42+
for (i = k - 1; i >= 0; i--) {
43+
await Animation.getAnimation(4);
44+
const oldValue = array[i]; // value being sorted to a new index
45+
const oldIndex = this.getIndexDOM(oldValue, DOM); // index of old value
46+
const sortedIndex = bucket[Math.floor(array[i] / exp) % 100] - 1;
47+
const valueInOldArray = array[sortedIndex];
48+
49+
Animation.swap(oldIndex, sortedIndex);
50+
51+
sortedArray[sortedIndex] = oldValue;
52+
bucket[Math.floor(array[i] / exp) % 100]--;
53+
}
54+
55+
for (i = 0; i < k; i++) {
56+
array[i] = sortedArray[i];
57+
}
58+
}
59+
60+
async perform(options, array) {
61+
let max = this.getMaxValue(array);
62+
let DOM = Array.from(document.querySelectorAll('.array-bars'));
63+
let originalArray = array.slice();
64+
65+
for(let i = 1; Math.floor(max / i) > 0; i*=100) {
66+
await this.sort(array, i, DOM, originalArray);
67+
}
68+
}
69+
}
70+
71+
export { RadixSortLSD }

‎src/components/Strategy/Algorithms/SelectionSort.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default class SelectionSort {
5858
}
5959

6060
// SG 07/08/2022 13:09 SWAP Animation - swaps element id and height
61-
this.Animation.swap(i, minBarIndex);
61+
Animation.swap(i, minBarIndex);
6262

6363
unSortedBars[i].style.backgroundColor = SORTAED;
6464
}

0 commit comments

Comments
 (0)