It searches for x in arr[], and partitions the array around x
| 6 | |
| 7 | // It searches for x in arr[], and partitions the array around x |
| 8 | int partition(int arr[], int l, int r, int x) { |
| 9 | // Search for x in arr[] and move it to end |
| 10 | int i; |
| 11 | for (i = l; i < r; i++) |
| 12 | if (arr[i] == x) |
| 13 | break; |
| 14 | swap(arr[i], arr[r]); |
| 15 | |
| 16 | // Standard partition algorithm |
| 17 | i = l; |
| 18 | for (int j = l; j <= r - 1; j++) { |
| 19 | if (arr[j] <= x) { |
| 20 | swap(arr[i], arr[j]); |
| 21 | i++; |
| 22 | } |
| 23 | } |
| 24 | swap(arr[i], arr[r]); |
| 25 | return i; |
| 26 | } |
| 27 | |
| 28 | // A simple function to find median of arr[] |
| 29 | int findMedian(int arr[], int n) { |