-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection Algorithm.cpp
More file actions
162 lines (136 loc) · 3.86 KB
/
Selection Algorithm.cpp
File metadata and controls
162 lines (136 loc) · 3.86 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Created by: Nikhil Shaw
Date of creation: 29 Sep 2016
Aim: To find the Kth smallest element in an unsorted array of numbers
*/
/*
arr: Unsorted array of numbers.
k: Kth smallest element in the array.
n: Total number of elements in the unsorted array.
Ns: Closest elememt smaller than Kth element encountered.
Nl: Closest element greater than Kth element encountered.
smallcount: Keeps count of elements smaller than arr[j].
largecount: Keeps count of elements larger than arr[j].
smalllimit: Number of possible elements smaller than Kth element.
largelimit: Number of possible elements greater than Kth element.
*/
/*
Algorithm:
1. Start
2. Input the unsorted array (arr[]), its size (n) and value of k (Kth smallest element)
3. smalllimit=k-1
largelimit-n-k
Ns= -infinity (minimum value possible)
Nl= +infinity (maximum value possible)
4. Note: Keep count of smallcount, largecount, smalllimit, largelimit, Ns and Nl
from i=0 till i<n:
{
smallcount=largecount=0
j=i+1
if i==n-1 then:
print the element (this is the Kth element)
if arr[j] doesn't lie between Ns and Nl, then:
{
if arr[j]<Ns then:
decrement smalllimit by 1
else:
decrement largelimit by 1
i++
continue the loop
}
from j=i+1 till j<n:
{
if arr[j] < arr[i] then:
increment smallcount by 1
else:
increment largecount by 1
if smallcount>smalllimit then:
Decrement largelimit by 1
Nl= arr[i]
break out of the loop
else if largecount > largelimit:
Decrement smalllimt by 1
Ns= arr[i]
break out of the loop
Increment j by 1
}
if smallcount is equal to smalllimit and largecount is equal to largelimit
print the element in the array (ie. print arr[i])
}
5. End
*/
/*
Following is the code in C++
*/
#include<iostream>
using namespace std;
int main()
{
int arr[20], smalllimit, largelimit, smallcount, largecount, Ns, Nl, k, n, i, j;
cout<<"Enter the number of elements in the unsorted array"<<endl;
cin>>n;
cout<<"Enter the unsorted array"<<endl;
for(i=0;i<n;i++)
cin>>arr[i];
cout<<"Enter the Kth smallest element in the array you want to find"<<endl;
cin>>k;
while((k<1)||(k>n)) // condition for valid value of k
{
cout<<"Enter K between 1 and "<<n<<endl;
cin>>k;
}
smalllimit=k-1;
largelimit=n-k;
Ns=-30000; // very low value initially
Nl=30000; // very high value initially
for(i=0;i<n;i++)
{
smallcount=0;
largecount=0;
j=i+1;
if(i==n-1) // Only one element left in the array to process
{
cout<<"The element is "<<arr[i];
break;
}
else if( (arr[i]>Nl)||(arr[i]<Ns) ) // if element doesn't lie between Ns and Nl
{
if(arr[i]<Ns)
{
smalllimit--;
}
else
largelimit--;
continue;
}
else
{
for(j=i+1;j<n;j++)
{
if(arr[j]<arr[i])
smallcount++;
else
largecount++;
if(smallcount>smalllimit)
{
largelimit--;
Nl=arr[i];
break;
}
else if(largecount>largelimit)
{
smalllimit--;
Ns=arr[i];
break;
}
else{}
}
}
if( (smallcount==smalllimit)&&(largecount==largelimit) ) // It is the Kth elements with exactly k-1 elements smaller than it and n-k elements greter than it.
{
cout<<"The element is "<<arr[i];
break;
}
}
return 0;
}