MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / partition

Function partition

CPP/sorting/randomised_quicksort.cpp:10–28  ·  view source on GitHub ↗

This function places all smaller to left of pivot and all greater elements to right of pivot

Source from the content-addressed store, hash-verified

8// to left of pivot and all greater
9// elements to right of pivot
10int 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
31int partition_r(int arr[], int low, int high) {

Callers 1

partition_rFunction · 0.70

Calls 1

swapFunction · 0.70

Tested by

no test coverage detected