From 5e90ffe0f0534a29d3c85dfc53a16d3871210177 Mon Sep 17 00:00:00 2001 From: shabazaps <66626010+shabazaps@users.noreply.github.com> Date: Sun, 10 Mar 2024 15:29:34 +0530 Subject: [PATCH] Update CC_QuickSort.pde Updated the CC_QuickSort.pde file for better readability and performance --- CC_QuickSort/CC_QuickSort.pde | 93 +++++++++++------------------------ 1 file changed, 30 insertions(+), 63 deletions(-) diff --git a/CC_QuickSort/CC_QuickSort.pde b/CC_QuickSort/CC_QuickSort.pde index b6ca0a1..563641e 100644 --- a/CC_QuickSort/CC_QuickSort.pde +++ b/CC_QuickSort/CC_QuickSort.pde @@ -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(); }