-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab2_k-smallest.cpp
More file actions
47 lines (43 loc) · 1.03 KB
/
lab2_k-smallest.cpp
File metadata and controls
47 lines (43 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
int partition(int arr[], int p, int q){
int z = p-1;
int pivot = arr[q];
for(int j = p; j < q; j++){
if(arr[j]<pivot){
z++;
swap(arr[z],arr[j]);
}
}
swap(arr[z+1],arr[q]);
return z+1;
}
int randomize_partition(int arr[],int p, int q){
int t = p + rand()%(q-p+1);
swap(arr[t],arr[q]);
return(partition(arr,p,q));
}
void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
int find(int arr[], int p, int r, int k){
if(k > 0 && k <= r - p + 1){
int q = randomize_partition(arr,p,r);
if(k-1 == q-p){
return arr[q];
}
if(k-1 < q-p){
return find(arr,p,q-1,k);
}
return find(arr,q+1,r,k+p-q-1);
}
return INT_MAX;
}
int main(){
srand(time(NULL));
int arr[20] = {10,40,30,50,70,20,90,80,100,60,110,150,120,160,130,170,140,180,200,190};
cout<<find(arr,0,19,11);
return 0;
}