Skip to content

Instantly share code, notes, and snippets.

@feehe21
Created January 15, 2020 18:41
Show Gist options
  • Save feehe21/f0f0a5b721d37399239c23b9d9dee4a7 to your computer and use it in GitHub Desktop.
Save feehe21/f0f0a5b721d37399239c23b9d9dee4a7 to your computer and use it in GitHub Desktop.
int[] a = new int[255];
ArrayList<Integer> left = new ArrayList<Integer>();
int x = 0;
int xw = 0;
int y = 0;
boolean swapOccured = true;
int i = 0;
void setup(){
size(1275,400);
setArr();
frameRate(10);
displayA();
}
void draw(){
if(swapOccured){
bubbleSort();
}
}
void bubbleSort(){
swapOccured = false;
i++;
for (int j = 0; j < a.length - i; j++) {
if (a[j] > a[j + 1]) {
swap(a, j, j + 1);
swapOccured = true;
}
}
displayA();
}
public void swap(int[] x, int i, int j){
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
void setArr(){
int q = 0;
for(int i = 0; i < 255; i++){
left.add(i);
}
for(int i = 0; i < 255; i++){
q = (int)left.size();
q = (int)random(q);
a[i] = left.remove(q);
}
}
void displayA(){
x=0;
xw = width/255;
for(int i = 0; i<a.length;i++){
fill(0,a[254-i],a[i]);
stroke(0,0,0);
rect(x,y,xw,height);
x += xw;
}
}
//display array using rectanlges
void displayArr(){
int x = 10;//change these to fit your needs
int y = 10;
fill(255,0,0);
rect(x,y,width,height);
}
void mousePressed(){
bubbleSort();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment