-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindrank.cpp
83 lines (66 loc) · 1.5 KB
/
findrank.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<cstdlib>
using namespace std;
int randompivot(int *arr,int i,int j)
{
srand ( time(NULL) );
int p=rand()%(j-i+1);
return (i+p);
}
int partition(int *arr,int i,int j,int p)
{
int l=i;
int r=j;
while(l<r)
{
while(arr[l]<=arr[p]&&l<p)
l++;
while(arr[r]>=arr[p]&&r>p)
r--;
if(l<r)
{
if(l==p) { p=r;}
else if(r==p) { p=l;}
int temp=arr[l];
arr[l]=arr[r];
arr[r]=temp;
}
}
cout<<"\npartition is with respect to random element: "<<arr[p]<<"\n";
for(int k=i;k<=j;k++)
cout<<arr[k]<<" ";
cout<<"\n";
return p;
}
int findrank(int *arr, int i, int j, int r)
{ int ans;
// If k is smaller than number of elements in array
if (r > 0 && r <= j - i + 1)
{
int p=randompivot(arr,i,j);
int pos=partition(arr,i,j,p);
cout<<p<<" "<<pos<<" ";
// If position is same as k
if (j-pos == r-1)
{ return arr[pos];}
else if (j-pos > r-1) // If position is more, recur for right subarray
{ return findrank(arr, pos+1, j, r);}
else{
// Else recur for left subarray
return findrank(arr, i, pos-1, r-(j-pos+1));}
}
// If k is more than number of elements in array
return 345;
}
int main()
{
int r, arr[]={1,67,8,4,56,90,4,87,43};
cout<<"original array"<<endl;
int k=(sizeof(arr)/sizeof(*arr));
for(int i=0;i<k;i++)
cout<<arr[i]<<" ";
cout<<"enter rank to find element : ";
cin>>r;
cout<<"\n element is "<<findrank(arr,0,k-1,r)<<"\n";
return 0;
}