Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. /*
  2. * Utility function used to swap floats.
  3. */
  4. void swapFloat(float* a, float* b){
  5. float t = *a;
  6. *a = *b;
  7. *b = t;
  8. }
  9.  
  10. /*
  11. * Utility function used to swap integers.
  12. */
  13. void swapInt(int* a, int* b){
  14. int t = *a;
  15. *a = *b;
  16. *b = t;
  17. }
  18.  
  19. /*
  20. * Utility function for quicksort.
  21. */
  22. int partition (float arr[], int low, int high, int arr2[]){
  23. float pivot = arr[high]; // pivot
  24. int i = (low - 1); // Index of smaller element
  25. for (int j = low; j <= high- 1; j++){
  26. // If current element is smaller than or
  27. // equal to pivot
  28. if (arr[j] > pivot){
  29. i++; // increment index of smaller element
  30. swapFloat(&arr[i], &arr[j]);
  31. swapInt(&arr2[i], &arr2[j]);
  32. }
  33. }
  34. swapFloat(&arr[i + 1], &arr[high]);
  35. return (i + 1);
  36. }
  37.  
  38. /*
  39. * Main Quicksort function.
  40. */
  41. void quickSort(float arr[], int low, int high, int arr2[]){
  42. if (low < high){
  43. /* pi is partitioning index, arr[p] is now
  44. at right place */
  45. int pi = partition(arr, low, high, arr2);
  46.  
  47. // Separately sort elements before
  48. // partition and after partition
  49. quickSort(arr, low, pi - 1, arr2);
  50. quickSort(arr, pi + 1, high, arr2);
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement