Skip to content

Commit 9ab9682

Browse files
authored
Merge pull request div-bargali#797 from rachitmanas/main
Added algorithm to Search kth smallest in O(n) time
2 parents d7563eb + 22ddfaf commit 9ab9682

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// C++ implementation of worst case linear time algorithm
2+
// to find k'th smallest element
3+
#include<iostream>
4+
#include<algorithm>
5+
#include<climits>
6+
7+
using namespace std;
8+
9+
int partition(int arr[], int l, int r, int k);
10+
11+
// A simple function to find median of arr[]. This is called
12+
// only for an array of size 5 in this program.
13+
int findMedian(int arr[], int n)
14+
{
15+
sort(arr, arr+n); // Sort the array
16+
return arr[n/2]; // Return middle element
17+
}
18+
19+
// Returns k'th smallest element in arr[l..r] in worst case
20+
// linear time. ASSUMPTION: ALL ELEMENTS IN ARR[] ARE DISTINCT
21+
int kthSmallest(int arr[], int l, int r, int k)
22+
{
23+
// If k is smaller than number of elements in array
24+
if (k > 0 && k <= r - l + 1)
25+
{
26+
int n = r-l+1; // Number of elements in arr[l..r]
27+
28+
// Divide arr[] in groups of size 5, calculate median
29+
// of every group and store it in median[] array.
30+
int i, median[(n+4)/5]; // There will be floor((n+4)/5) groups;
31+
for (i=0; i<n/5; i++)
32+
median[i] = findMedian(arr+l+i*5, 5);
33+
if (i*5 < n) //For last group with less than 5 elements
34+
{
35+
median[i] = findMedian(arr+l+i*5, n%5);
36+
i++;
37+
}
38+
39+
// Find median of all medians using recursive call.
40+
// If median[] has only one element, then no need
41+
// of recursive call
42+
int medOfMed = (i == 1)? median[i-1]:
43+
kthSmallest(median, 0, i-1, i/2);
44+
45+
// Partition the array around a random element and
46+
// get position of pivot element in sorted array
47+
int pos = partition(arr, l, r, medOfMed);
48+
49+
// If position is same as k
50+
if (pos-l == k-1)
51+
return arr[pos];
52+
if (pos-l > k-1) // If position is more, recur for left
53+
return kthSmallest(arr, l, pos-1, k);
54+
55+
// Else recur for right subarray
56+
return kthSmallest(arr, pos+1, r, k-pos+l-1);
57+
}
58+
59+
// If k is more than number of elements in array
60+
return INT_MAX;
61+
}
62+
63+
void swap(int *a, int *b)
64+
{
65+
int temp = *a;
66+
*a = *b;
67+
*b = temp;
68+
}
69+
70+
// It searches for x in arr[l..r], and partitions the array
71+
// around x.
72+
int partition(int arr[], int l, int r, int x)
73+
{
74+
// Search for x in arr[l..r] and move it to end
75+
int i;
76+
for (i=l; i<r; i++)
77+
if (arr[i] == x)
78+
break;
79+
swap(&arr[i], &arr[r]);
80+
81+
// Standard partition algorithm
82+
i = l;
83+
for (int j = l; j <= r - 1; j++)
84+
{
85+
if (arr[j] <= x)
86+
{
87+
swap(&arr[i], &arr[j]);
88+
i++;
89+
}
90+
}
91+
swap(&arr[i], &arr[r]);
92+
return i;
93+
}
94+
95+
// Driver program to test above methods
96+
int main()
97+
{
98+
int n,k;
99+
cout<<"Enter size of the array: ";
100+
cin>>n;
101+
cout<<"Enter The elements of array\n";
102+
int arr[n];
103+
for (int i = 0; i < n; i++)
104+
{
105+
cin>>arr[i];
106+
}
107+
cout<<"Enter k: ";//Program will find kth smallest value in array without sorting
108+
cin>>k;
109+
cout << "K'th smallest element is "
110+
<< kthSmallest(arr, 0, n-1, k);
111+
return 0;
112+
}

0 commit comments

Comments
 (0)