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

Function partition

CPP/sorting/quicksort.cpp:18–35  ·  view source on GitHub ↗

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 */

Source from the content-addressed store, hash-verified

16to left of pivot and all greater elements to right
17of pivot */
18int 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
38arr[] --> Array to be sorted,

Callers 2

quickSortFunction · 0.70
sortitMethod · 0.50

Calls 1

swapFunction · 0.70

Tested by

no test coverage detected