xxxxxxxxxx
96
//unccomment which sorting algorithm
// const sort = "BUBBLE";
const sort = "SELECTION";
let l = [];
let len = 384;
let cols = [];
let c2 = [147, 10, 245] //purple
let c1 = [255, 255, 0] //yellow
function getColor(i) {
return [map(i,0,len,c1[0],c2[0]),
map(i,0,len,c1[1],c2[1]),
map(i,0,len,c1[2],c2[2])];
}
function setup() {
createCanvas(len, 500);
for (let i=0;i<len;i++) {
l.push(i);
cols.push(getColor(i));
}
shuffle(l,true);
frameRate(15);
// colorMode(HSB);
noStroke();
}
function mousePressed() {
fullscreen(true);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
len=width;
cols=[];
l=[]
for (let i=0;i<len;i++) {
l.push(i);
cols.push(getColor(i));
}
shuffle(l,true);
i=len;
}
let i = len;
function selectionSort(arr) {
let a = arr;
let biggest_i=i;
let biggest=l[i];
for (let j=i;j>=0;j--) {
if (l[j]>=biggest) {
biggest = l[j];
biggest_i=j;
}
}
tmp = l[i];
l[i]=biggest;
l[biggest_i]=tmp;
i--;
return a;
}
let has_swapped=false;
let ind = 0;
function bubble(arr) {
let a = arr;
if (!(ind==len-1 && has_swapped==false)) {
if (a[ind]>a[ind+1]) {
tmp = a[ind];
a[ind]=a[ind+1];
a[ind+1]=tmp;
has_swapped=true;
}
ind++;
if (ind>=len){ind=0;has_swapped=false;}
}
return a;
}
let n = 1;
function draw() {
for (let i=0;i<10;i++) {
if (sort=="SELECTION"){l=selectionSort(l);}
else if (sort=="BUBBLE"){l=bubble(l);}
}
background(0);
for (let i=0;i<len;i++) {
h=(l[i]+1)/len*height
//fire
// let c =l[i]/len*360%65;
// fill(c,100,100)
// if (l[i]/len*360%(65*2)>65) {fill(65-c,100,100)}
stroke(cols[i][0],cols[i][1],cols[i][2]);
line(map(i,0,len,0,width),height-h,map(i,0,len,0,width),height);
}
fill(255)
text(width+", "+height,width/2,height/2)
n++
}