xxxxxxxxxx
80
let len = 5;
let arr = [];
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function quickSort(arr, start, end){
if(start >= end) return;
let index = partition(arr, start, end);
quickSort(arr, start, index - 1);
quickSort(arr, index + 1, end);
}
async function swap(arr, a, b){
await wait(25);
let temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
async function partition(arr, start, end){
let pivotIndex = start;
let pivotValue = arr[end];
for(let i = start; i < end; i++){
if(arr[i] < pivotValue) {
await swap(arr, i, pivotIndex);
pivotIndex++;
}
}
await swap(arr, pivotIndex, end);
return pivotIndex;
}
function setup() {
createCanvas(800, 800);
for(let i = 0; i < 160; i++){
arr[i] = random(height);
}
}
function draw() {
background(240, 120, 120);
quickSort(arr, 0, arr.length - 1);
for(let i = 0; i < arr.length; i++){
noStroke();
fill(0);
rect(i * len, height - arr[i], len, arr[i]);
}
}
class Rects {
constructor(x, y, h){
this.x = x;
this.y = y;
this.len = len;
this.h = h;
this.col = random(255);
}
show(){
noStroke();
fill(this.col);
rect(this.x, this.y, this.len, this.h);
}
}