-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathbucketSort.ts
More file actions
125 lines (111 loc) Β· 3.4 KB
/
Copy pathbucketSort.ts
File metadata and controls
125 lines (111 loc) Β· 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Bucket Sort β distributes into buckets, insertion-sorts each, concatenates.
*
* Time: O(n + nΒ²/k) average; O(nΒ²) worst when one bucket holds all.
* Space: O(n + k).
*/
import { delayStep } from '@/algorithms/sleep';
import type {
ShouldStopRef,
SortingAlgorithm,
SortingAlgorithmAudio,
SortStepDelayRefs,
SortStepMetrics,
} from '@/algorithms/types';
async function insertionSortBucket(
bucket: number[],
shouldStopRef: ShouldStopRef,
audio: SortingAlgorithmAudio,
metrics: SortStepMetrics
): Promise<void> {
for (let i = 1; i < bucket.length; i++) {
if (shouldStopRef.current) {
return;
}
const key = bucket[i];
let j = i - 1;
while (j >= 0 && bucket[j] > key) {
metrics.comparisons++;
audio.playCompareSound(bucket[j]);
bucket[j + 1] = bucket[j];
j--;
metrics.swaps++;
}
bucket[j + 1] = key;
audio.playSwapSound(key);
}
}
export const bucketSort: SortingAlgorithm = async (
array,
visualizeArray,
delay,
setCurrentBar,
shouldStopRef,
sortPausedRef,
audio,
delayRef
) => {
const delayRefs: SortStepDelayRefs = {
shouldStopRef,
sortPausedRef,
delayRef,
};
const metrics: SortStepMetrics = { swaps: 0, comparisons: 0 };
if (array.length === 0) {
setCurrentBar({ compare: null, swap: null });
audio.playCompleteSound();
return metrics;
}
if (array.length === 1) {
setCurrentBar({ compare: null, swap: null });
audio.playCompleteSound();
return metrics;
}
const max = Math.max(...array);
const min = Math.min(...array);
const range = max - min;
const bucketCount = Math.max(1, Math.floor(Math.sqrt(array.length)));
const bucketSpan = range === 0 ? 1 : range / bucketCount;
const buckets: number[][] = Array.from({ length: bucketCount }, () => []);
for (let i = 0; i < array.length; i++) {
if (shouldStopRef.current) {
setCurrentBar({ compare: null, swap: null });
return metrics;
}
const bucketIndex = Math.floor((array[i] - min) / bucketSpan);
const safeIndex = Math.min(Math.max(0, bucketIndex), bucketCount - 1);
buckets[safeIndex].push(array[i]);
setCurrentBar({ compare: i, swap: null });
visualizeArray([...array]);
audio.playAccessSound(array[i]);
// react-doctor-disable-next-line -- intentionally sequential for visualization, react-doctor/async-await-in-loop
await delayStep(delay, delayRefs);
}
for (let i = 0; i < buckets.length; i++) {
if (shouldStopRef.current) {
setCurrentBar({ compare: null, swap: null });
return metrics;
}
// react-doctor-disable-next-line -- intentionally sequential for visualization, react-doctor/async-await-in-loop
await insertionSortBucket(buckets[i], shouldStopRef, audio, metrics);
}
let index = 0;
for (let i = 0; i < buckets.length; i++) {
if (shouldStopRef.current) {
setCurrentBar({ compare: null, swap: null });
return metrics;
}
for (let j = 0; j < buckets[i].length; j++) {
array[index] = buckets[i][j];
setCurrentBar({ compare: null, swap: index });
visualizeArray([...array]);
audio.playAccessSound(array[index]);
// react-doctor-disable-next-line -- intentionally sequential for visualization, react-doctor/async-await-in-loop
await delayStep(delay, delayRefs);
index++;
}
}
setCurrentBar({ compare: null, swap: null });
audio.playCompleteSound();
return metrics;
};