Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Utility function used to swap floats.
- */
- void swapFloat(float* a, float* b){
- float t = *a;
- *a = *b;
- *b = t;
- }
- /*
- * Utility function used to swap integers.
- */
- void swapInt(int* a, int* b){
- int t = *a;
- *a = *b;
- *b = t;
- }
- /*
- * Utility function for quicksort.
- */
- int partition (float arr[], int low, int high, int arr2[]){
- float pivot = arr[high]; // pivot
- int i = (low - 1); // Index of smaller element
- for (int j = low; j <= high- 1; j++){
- // If current element is smaller than or
- // equal to pivot
- if (arr[j] > pivot){
- i++; // increment index of smaller element
- swapFloat(&arr[i], &arr[j]);
- swapInt(&arr2[i], &arr2[j]);
- }
- }
- swapFloat(&arr[i + 1], &arr[high]);
- return (i + 1);
- }
- /*
- * Main Quicksort function.
- */
- void quickSort(float arr[], int low, int high, int arr2[]){
- if (low < high){
- /* pi is partitioning index, arr[p] is now
- at right place */
- int pi = partition(arr, low, high, arr2);
- // Separately sort elements before
- // partition and after partition
- quickSort(arr, low, pi - 1, arr2);
- quickSort(arr, pi + 1, high, arr2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement