Created
July 12, 2022 08:04
-
-
Save techisbeautiful/961fb68ea80c27d6a22b4a649cb1cf9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void swap(int[] arr, int i, int j) { | |
int temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
} | |
static int partition(int[] arr, int low, int high) { | |
// Pick pivot | |
int pivot = arr[high]; | |
// Index of smaller element and indicates the right position of pivot found so far | |
int i = (low - 1); | |
for(int j = low; j <= high - 1; j++) { | |
// If current element is smaller than the pivot | |
if (arr[j] < pivot) { | |
i++; | |
swap(arr, i, j); | |
} | |
} | |
swap(arr, i + 1, high); | |
return (i + 1); | |
} | |
static void quickSort(int[] dataCollection, int low, int high) { | |
if (low < high) { | |
// p is partitioning index, arr[p] is now at right place | |
int p = partition(dataCollection, low, high); | |
// Separately sort elements before partition and after partition | |
quickSort(dataCollection, low, p - 1); | |
quickSort(dataCollection, p + 1, high); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment