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

Function partition

CPP/arrays/KthSmallest.cpp:8–26  ·  view source on GitHub ↗

It searches for x in arr[], and partitions the array around x

Source from the content-addressed store, hash-verified

6
7// It searches for x in arr[], and partitions the array around x
8int 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[]
29int findMedian(int arr[], int n) {

Callers 1

kthSmallestFunction · 0.70

Calls 1

swapFunction · 0.50

Tested by

no test coverage detected