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

Added Stack iteration quick sort for visualization. #9

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
179 changes: 108 additions & 71 deletions CC_QuickSort/CC_QuickSort.pde
Original file line number Diff line number Diff line change
@@ -1,90 +1,127 @@
import java.util.Stack;

float[] values;
//Array to store image hues
float[] numberArray;

void setup() {
size(600, 400);
values = new float[width];
for (int i = 0; i < values.length; i++) {
values[i] = random(height);
}
noLoop();
}
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);
}
}

// Working partition code from:
// https://www.geeksforgeeks.org/quick-sort/
//How many times to step in the sort algo per frame.
int stepSize;

//int partition (float arr[], int low, int high) {
// float pivot = arr[high];
// int i = (low - 1);
// for (int j = low; j <= high- 1; j++) {
// if (arr[j] <= pivot) {
// i++;
// swap(arr, i, j);
// }
// }
// swap(arr, i+1, high);
// return (i + 1);
//}
//Needed for the version of the iterative quick sort that I used (thanks https://javarevisited.blogspot.com/2016/09/iterative-quicksort-example-in-java-without-recursion.html)
Stack stack;

void mousePressed() {
quicksort(values, 0, values.length-1);
void setup(){
//Fits on my screen
size(1000, 1000);
background(0);
reset();
}

void draw() {
render();
println("Help");
void reset(){

//create Array
numberArray = new float[width];

//Fill Array
for (int i = 0; i < numberArray.length; i++){
numberArray[i] = random(height);
}

//Make a stack and push initial values to it.
stack = new Stack();
stack.push(0);
stack.push(numberArray.length);

//Create a step size change the divisor higher for smaller steps but quicker animation and lower for less steps but quicker sorting, within reason.
stepSize = 1;
if(stepSize < 1) stepSize = 1;

}

// Broken partition code that I tried to write
// TODO: Help!
int partition(float[] arr, int lo, int hi) {
//println("Partition " + lo + " to " + hi);
float pivot = arr[hi];
int left = lo-1;
int right = hi-1;

while (left <= right) {
left++;
println(left, right);
if (arr[left] >= pivot) {
while (right > left) {
if (arr[right] < pivot) {
swap(arr, left, right);
break;
void draw(){
background(0);
stroke(255);
//Render array
for (int i = 0; i < numberArray.length; i++){
line(i, height, i, numberArray[i]);
}

//Make sure the stack has stuff in it to use.
if(!stack.isEmpty()){
for (int i = 0; i < stepSize; i++){
//again for the done message to get called
if(!stack.isEmpty()){
//Set end and start to equal the current stack's knowledge of the array. initially the length of the array and then 0
int end = (int) stack.pop();
int start = (int) stack.pop();

//Make sure that the ends have no crossed each other or are not already right next to each other.
if(end - start >= 2){
//pick the pivot for the partition algo
int p = (int) random((float) start, (float) end);
//Set P the the returned value from the partition algo
p = Part(numberArray, p, start, end);

//add to the stack the return part value + 1. Similar to a recursive call with an increased start
stack.push(p + 1);
stack.push(end);

//add to the stack the return part value + 1 Similar to a recursive call with a decreased end
stack.push(start);
stack.push(p);

//The stack is constantly moving the two "ends" being tested together eventual they will be right next to each other and the stack will be empty because nothing new will be added.
}
right--;
}else{
//Look at the we done sorted!!
System.out.println("DONE!");
//reset();
break;
}
}
}

if (left < hi-1) {
swap(arr, left, hi);
}
println("Mid: "+ left);
return left;
}

//Partition Algo
public int Part(float[] input, int position, int start, int end){
// Start and end for the current "segement" of the array we are testing
int l = start;
int h = end - 2;

//The pivot or value we are using to compare others in the "segement" to.
float piv = input[position];

//Put pivot at the end.
swap(input, position, end - 1);

void render() {
background(0);
for (int i = 0; i < values.length; i++) {
stroke(255);
line(i, height, i, height - values[i]);
//While where we have started is not where we are meant to end.
while (l < h) {
//Since pivot is already at the end if the value we are at the beginning testing is less than the pivot, do nothing ubt increase where we are starting
if (input[l] < piv) {
l++;
//Since pivot is already at the end if the value we are testing at the end is more than the pivot, do nothing but decrease where we are ending
} else if (input[h] >= piv) {
h--;
} else {
//If the value at the begining is more than the pivot or the value at the end is less than the pivot swap'em.
swap(input, l, h);
}
}

//now our return should be the top most value
int idx = h;
//If the top most value is less than the pivot increase our return value.
if (input[h] < piv) {
idx++;
}
//Put our return value on the end of the "segement"
swap(input, end - 1, idx);
//return the value to continue the algo
return idx;
}

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


redraw();
//Utility to swap array entries
public void swap(float[] arr, int i, int j) {
float temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}