-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquickLomuto.js
More file actions
46 lines (42 loc) · 1.33 KB
/
quickLomuto.js
File metadata and controls
46 lines (42 loc) · 1.33 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
function QuickSort() {
this.name = 'Quick sort';
this.count = 40;
this.representation = 1;
this.sorter = function* (arr, colors, start = 0, end = arr.length - 1, self = this) {
if (start < end) {
// Partition list with pivot
let ref = {};
yield* self['helpers']['partition'](arr, colors, start, end, ref);
let p = ref[''];
// Sort parts recursively
yield* self['sorter'](arr, colors, start, p - 1);
yield* self['sorter'](arr, colors, p + 1, end);
} else {
colors[start] = color(128, 255, 51);
}
return colors;
};
this.helpers = {
partition: function* (arr, colors, start, end, i) {
let pivot = arr[end];
i[''] = start;
for (let j = i['']; j < end; ++j) {
myDelay(100);
passes++;
compares++;
if (arr[j] < pivot) {
colors[i['']] = color('red');
colors[j] = color('red');
yield colors;
colors[i['']] = color('white');
colors[j] = color('white');
swap(arr, i['']++, j);
swaps++;
}
}
swap(arr, i[''], end);
swaps++;
colors[i['']] = color(128, 255, 51);
}
}
}