Skip to content

Instantly share code, notes, and snippets.

@raunaqsingh2020
Created January 17, 2020 14:59
Show Gist options
  • Save raunaqsingh2020/8b8887811c57e0a858d65d9aab7de99f to your computer and use it in GitHub Desktop.
Save raunaqsingh2020/8b8887811c57e0a858d65d9aab7de99f to your computer and use it in GitHub Desktop.
int[] arr = new int[140];
//int alpha=0;
int w = 10;
int h = 400;
boolean sorting = false;
void setup(){
size(1400,400);
setArr();
frameRate(10);
displayArr();
}
void draw(){
if(sorting){
//alpha+=1;
bubbleSort();
}
}
void setArr(){
for(int i = 0; i < arr.length; i++){
arr[i]=(int)random(1,255);
}
}
//selection sort
public void bubbleSort(){
int i = 0;
i++;
for (int j = 0; j < arr.length - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
//display array using rectanlges
void displayArr(){
//System.out.println(Arrays.toString(arrayStr));
for(int i = 0; i < arr.length; i++){
//noStroke();
//fill(30,arr[i]/2,arr[i]);
fill(arr[i],20,255-arr[i]);
rect(10*i,0,w,h);
}
}
void mousePressed(){
//bubbleSort();
sorting=!sorting;
}
public void swap(int[] x, int i, int j){
int temp = x[i];
x[i] = x[j];
x[j] = temp;
displayArr();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment