xxxxxxxxxx
60
let blocks = [];
const count = 1000;
function setup() {
createCanvas(400, 300);
Create();
frameRate(1)
}
function draw() {
background(220);
Show();
Sort();
}
function Sort() {
for (let i = 0; i < blocks.length; i++) {
let current = blocks[i]
if (i == 0) continue;
Check(current, i);
}
}
function Check(c, i) {
if (c > blocks[i-1]) {
blocks = Swap(blocks, i, i-1)
// background(220);
Show();
Check(c, i-1);
}
return;
}
function Swap(arr, a, b) {
let temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
return arr;
}
function Show() {
noStroke()
for (let i = 0; i < blocks.length; i++) {
fill(blocks[i]-200,blocks[i]-00,blocks[i]-100)
rect(i*(width/blocks.length), height-blocks[i], width/blocks.length,blocks[i])
}
}
function Create() {
for (let i = 0; i < count; i++) {
blocks[i] = random(30,height-40)
}
}
function mousePressed() {
Create();
}