-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
56 lines (54 loc) · 1.2 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
let arr = [];
let count = 0;
let check = false;
async function quickSort(arr, first, last) {
if (first < last) {
count++;
let pilot = arr[last];
let j = first - 1;
for (let i = first; i <= last - 1; i++) {
if (arr[i] < pilot) {
j++;
await swap(arr, i, j);
}
}
let pi = j + 1;
await swap(arr, pi, last);
Promise.all([quickSort(arr, first, pi - 1), quickSort(arr, pi + 1, last)]);
}
}
function run() {
loop();
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function swap(arr, i, j) {
await sleep(10);
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
function visual(arr) {
let len = arr.length
push();
translate(0, 400);
for (let i = 0; i < len; i++) {
fill(255 - arr[i]);
rect(i * (1600 / len), 0, 1600 / len, - arr[i]);
};
pop();
}
function setup() {
createCanvas(1600, 400).parent('sketch-holder');
for (let i = 0; i < 200; i++) {
arr.push(random(0, 255));
}
noLoop();
frameRate(60);
}
function draw() {
background(51);
visual(arr);
console.log(count);
}