Skip to content

Commit 93860fc

Browse files
committed
Radix sort
1 parent 7c20604 commit 93860fc

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Animation } from "../../Animation/Animation";
22

33

4+
// SG 07/10/2022 22:34 https://en.wikipedia.org/wiki/Radix_sort
5+
46
class RadixSortLSD {
57

68
getMaxValue(array) {
79
let max = array[0];
810
for(const num of array) {
911
if(max < num) max = num;
1012
}
11-
1213
return max;
1314
}
1415

@@ -18,7 +19,10 @@ class RadixSortLSD {
1819
}
1920
}
2021

21-
async sort(array, exp, DOM, originalArray) {
22+
async sort(array, exp, DOM, options, finalIteration) {
23+
const DEAULT_COLOR = options.defaultBarColor;
24+
const DELAY = options.delay;
25+
const SORTED_COLOR = options.sortedBarColor;
2226
console.log("================================================================");
2327
let k = array.length;
2428
let sortedArray = new Array(k);
@@ -38,32 +42,36 @@ class RadixSortLSD {
3842
bucket[i] += bucket[i - 1];
3943
}
4044

41-
// Build the sortedArray array
45+
4246
for (i = k - 1; i >= 0; i--) {
43-
await Animation.getAnimation(4);
47+
await Animation.getAnimation(DELAY);
4448
const oldValue = array[i]; // value being sorted to a new index
4549
const oldIndex = this.getIndexDOM(oldValue, DOM); // index of old value
4650
const sortedIndex = bucket[Math.floor(array[i] / exp) % 100] - 1;
4751
const valueInOldArray = array[sortedIndex];
4852

49-
Animation.swap(oldIndex, sortedIndex);
53+
Animation.swap(oldIndex, sortedIndex); // SG 07/10/2022 22:35 swap animation
54+
if(finalIteration) {
55+
DOM[oldIndex].style.backgroundColor = SORTED_COLOR;
56+
DOM[sortedIndex].style.backgroundColor = SORTED_COLOR;
57+
}
5058

5159
sortedArray[sortedIndex] = oldValue;
5260
bucket[Math.floor(array[i] / exp) % 100]--;
5361
}
5462

55-
for (i = 0; i < k; i++) {
63+
for (i = 0; i < k; i++) { // SG 07/10/2022 22:35 copying sorted values
5664
array[i] = sortedArray[i];
5765
}
5866
}
5967

6068
async perform(options, array) {
6169
let max = this.getMaxValue(array);
6270
let DOM = Array.from(document.querySelectorAll('.array-bars'));
63-
let originalArray = array.slice();
6471

6572
for(let i = 1; Math.floor(max / i) > 0; i*=100) {
66-
await this.sort(array, i, DOM, originalArray);
73+
// if((Math.floor(max / i*100) > 0) && i !== 1)
74+
await this.sort(array, i, DOM, options, ((Math.floor(max / i*100) > 0) && i !== 1));
6775
}
6876
}
6977
}

0 commit comments

Comments
 (0)