forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
34 lines (32 loc) · 1.03 KB
/
code.js
File metadata and controls
34 lines (32 loc) · 1.03 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
//place numbers into appropriate buckets
for (let i = 0; i < array.length; i++) {
var bucketPos = Math.floor(numBuckets * (array[i] / maxValue));
buckets[bucketPos].push(array[i]);
bucketsCount[bucketPos]++;
tracer._select(0, i)._wait();
tracer._notify(1, bucketPos, D[1][bucketPos])._wait();
tracer._deselect(0, i);
tracer._denotify(1, bucketPos, D[1][bucketPos]);
}
var sortLocation = 0;
for (let k = 0; k < buckets.length; k++) {
//do insertion sort
for (let i = 1; i < buckets[k].length; i++) {
var key = buckets[k][i];
var j;
for (j = i - 1; (j >= 0) && (buckets[k][j] > key); j--) {
buckets[k][j + 1] = buckets[k][j];
}
buckets[k][j + 1] = key;
}
//place ordered buckets into sorted array
for (let i = 0; i < buckets[k].length; i++) {
sortedArray[sortLocation] = buckets[k][i];
bucketsCount[k]--;
tracer._notify(1, k, D[1][k]);
tracer._notify(2, sortLocation, D[2][sortLocation])._wait();
tracer._denotify(1, k, D[1][k]);
tracer._denotify(2, sortLocation, D[2][sortLocation]);
sortLocation++;
}
}