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

Update CC_QuickSort.pde #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
93 changes: 30 additions & 63 deletions CC_QuickSort/CC_QuickSort.pde
Original file line number Diff line number Diff line change
@@ -1,90 +1,57 @@

float[] values;
float[] values; // Array to store values

void setup() {
size(600, 400);
values = new float[width];
size(600, 400); // Set canvas size
values = new float[width]; // Initialize values array with width of canvas
for (int i = 0; i < values.length; i++) {
values[i] = random(height);
values[i] = random(height); // Fill values array with random values
}
noLoop();
noLoop(); // Prevent draw() from looping
}

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);
int mid = partition(arr, lo, hi); // Get the partitioning index
quicksort(arr, lo, mid - 1); // Recursively sort elements before partition
quicksort(arr, mid + 1, hi); // Recursively sort elements after partition
}
}

// Working partition code from:
// https://www.geeksforgeeks.org/quick-sort/

//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);
//}

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

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

// Broken partition code that I tried to write
// TODO: Help!
// Partition function to select a pivot and rearrange the array
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;
}
right--;
}
float pivot = arr[hi]; // Choose the pivot element (last element)
int i = lo - 1; // Index of smaller element
for (int j = lo; j < hi; j++) {
if (arr[j] < pivot) {
i++;
swap(arr, i, j); // Swap arr[i] and arr[j]
}
}
swap(arr, i + 1, hi); // Swap arr[i+1] and arr[hi] (pivot)
return i + 1; // Return the partitioning index
}

if (left < hi-1) {
swap(arr, left, hi);
}
println("Mid: "+ left);
return left;
void mousePressed() {
quicksort(values, 0, values.length - 1); // Sort the values array when mouse is pressed
}

void draw() {
render(); // Render the sorted values
println("Help"); // Display a message in console
}

// Render function to display sorted values as lines
void render() {
background(0);
background(0); // Clear the background
for (int i = 0; i < values.length; i++) {
stroke(255);
line(i, height, i, height - values[i]);
stroke(255); // Set stroke color to white
line(i, height, i, height - values[i]); // Draw a line representing each value
}
}

// Swap function to exchange elements at two indices in an array
void swap(float[] arr, int a, int b) {
float temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;


redraw();
}