-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
83 lines (71 loc) · 1.6 KB
/
sketch.js
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
// Declaration Values
let values = [];
let w = 10;
let states = [];
// Setup Function
function setup() {
createCanvas(windowWidth, windowHeight);
values = new Array(floor(width / w));
for (let i = 0; i < values.length; i++) {
values[i] = random(height);
states[i] = -1;
}
quickSort(values, 0, values.length - 1);
}
async function quickSort(arr, start, end) {
if (start >= end) {
return;
}
let index = await partition(arr, start, end);
states[index] = -1;
await Promise.all([
quickSort(arr, start, index - 1),
quickSort(arr, index + 1, end),
]);
}
async function partition(arr, start, end) {
for (let i = start; i < end; i++) {
states[i] = 1;
}
let pivotValue = arr[end];
let pivotIndex = start;
states[pivotIndex] = 0;
for (let i = start; i < end; i++) {
if (arr[i] < pivotValue) {
await swap(arr, i, pivotIndex);
states[pivotIndex] = -1;
pivotIndex++;
states[pivotIndex] = 0;
}
}
await swap(arr, pivotIndex, end);
for (let i = start; i < end; i++) {
if (i != pivotIndex) {
states[i] = -1;
}
}
return pivotIndex;
}
function draw() {
background("#F3EFE0");
for (let i = 0; i < values.length; i++) {
noStroke();
if (states[i] == 0) {
fill("#222222");
} else if (states[i] == 1) {
fill("#22A39F");
} else {
fill("#434242");
}
rect(i * w, height - values[i], w, values[i]);
}
}
async function swap(arr, a, b) {
await sleep(50);
let temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}