Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contribution added to Quicksort_Viz #11

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions CC_QuickSort/QuickSort_Updated.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

float[] values;

int i, j;

void setup() {
size(600, 400);
values = new float[width];
for (int i = 0; i < values.length; i++) {
values[i] = random(height);
}
}

void quicksort(float[] arr, int lo, int hi) {
if (lo < hi) {
int mid = partition(arr, lo, hi);
quicksort(arr, lo, mid-1);
quicksort(arr, mid+1, hi);
}
}

void mousePressed() {
quicksort(values, 0, values.length-1);
}

void draw() {
background(0);
for (int i = 0; i < values.length; i++) {
stroke(255);
line(i, height, i, height - values[i]);
}
}

int partition (float arr[], int left, int right)
{
float pivot = arr[right]; // pivot
i = left - 1; // Index of smaller element

for(j = left; j <= right - 1; j++){
if( arr[j] <= pivot){
i++;
if(arr[i] != arr[j]){
swap(arr, i, j);
break;
}
}
}
swap(arr, i+1, j);
return i+1;
}

void swap(float[] arr, int a, int b) {
float temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
redraw();
}