-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubblSort.js
48 lines (40 loc) · 889 Bytes
/
bubblSort.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
let values = [];
let i = 0;
let j = 0;
let sorted = false;
function setup() {
createCanvas(windowWidth, windowHeight);
values = new Array(floor(width / 10)).fill().map(() => random(height));
frameRate(30);
}
function draw() {
background(30);
if (!sorted) {
if (i < values.length) {
for (let k = 0; k < values.length - i - 1; k++) {
let a = values[k];
let b = values[k + 1];
if (a > b) {
values[k] = b;
values[k + 1] = a;
}
}
i++;
} else {
sorted = true;
}
}
for (let i = 0; i < values.length; i++) {
stroke(255);
let col = map(values[i], 0, height, 0, 255);
fill(col, 100, 255 - col, 150);
rect(i * 10, height - values[i], 10, values[i], 5);
}
drawFrame();
}
function drawFrame() {
strokeWeight(4);
noFill();
stroke(255);
rect(0, 0, width, height);
}