xxxxxxxxxx
100
let n = 350;
let w = 0;
let bin = [];
let arr = [];
let i = 0;
let timeSteps = 1;
function jai_sort_v2(arr) {
for (let i = 0; i < n; i++) {
for (let j = n - 1; j >= i; j--) {
if (arr[i] > arr[j]) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
function jai_sort(arr) {
for (let i = 0; i < n;) {
let step = true;
for (let j = n - 1; j >= i; j--) {
if (arr[i] > arr[j]) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
step = false;
break;
}
}
if (step) i++;
}
}
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(60);
for (let i = 0; i < n; i++) {
bin[i] = i + 1;
}
for (let i = 0; i < n; i++) {
let r = random(bin);
let j = bin.indexOf(r);
arr[i] = r;
bin.splice(j, 1);
}
w = width/n;
// jai_sort(arr);
}
function draw() {
background(0);
rectMode(CENTER);
let x = w/2;
for (let t = 0; t < timeSteps; t++) {
let step = true;
let other = -1;
for (let j = n - 1; j >= i; j--) {
if (arr[i] > arr[j]) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
other = j;
step = false;
// break;
}
}
for (let j = 0; j < n; j++) {
let element = arr[j];
if (j == i) {
fill(0, 255, 0);
} else if (j == other) {
fill(255, 0, 0);
} else {
fill(255)
}
strokeWeight(1);
stroke(0);
rect(x, height - (element/n * height)/2, w, element/n * height);
x += w;
}
// if (step) i++;
i++;
}
fill(255);
textSize(15);
}