This function places all smaller to left of pivot and all greater elements to right of pivot
| 8 | // to left of pivot and all greater |
| 9 | // elements to right of pivot |
| 10 | int partition(int arr[], int low, int high) { |
| 11 | int pivot = arr[high]; |
| 12 | |
| 13 | // Index of smaller element |
| 14 | int i = (low - 1); |
| 15 | |
| 16 | for (int j = low; j <= high - 1; j++) { |
| 17 | // If current element is smaller |
| 18 | // than or equal to pivot |
| 19 | if (arr[j] <= pivot) { |
| 20 | |
| 21 | // increment index of smaller element |
| 22 | i++; |
| 23 | swap(arr[i], arr[j]); |
| 24 | } |
| 25 | } |
| 26 | swap(arr[i + 1], arr[high]); |
| 27 | return (i + 1); |
| 28 | } |
| 29 | |
| 30 | // Generates Random Pivot, swaps pivot with end element |
| 31 | int partition_r(int arr[], int low, int high) { |