This function takes last element as pivot, places the pivot element at its correct position in sorted array, and places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */
| 16 | to left of pivot and all greater elements to right |
| 17 | of pivot */ |
| 18 | int partition(int arr[], int low, int high) |
| 19 | { |
| 20 | int pivot = arr[high]; // pivot |
| 21 | int i |
| 22 | = (low |
| 23 | - 1); // Index of smaller element and indicates |
| 24 | // the right position of pivot found so far |
| 25 | |
| 26 | for (int j = low; j <= high - 1; j++) { |
| 27 | // If current element is smaller than the pivot |
| 28 | if (arr[j] < pivot) { |
| 29 | i++; // increment index of smaller element |
| 30 | swap(&arr[i], &arr[j]); |
| 31 | } |
| 32 | } |
| 33 | swap(&arr[i + 1], &arr[high]); |
| 34 | return (i + 1); |
| 35 | } |
| 36 | |
| 37 | /* The main function that implements QuickSort |
| 38 | arr[] --> Array to be sorted, |