-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathK'th smallest.cpp
56 lines (55 loc) · 1.19 KB
/
K'th smallest.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
#include<iostream>
using namespace std;
//function for sorting array in ascendeing order
void sort_array(int array[],int size)
{
int temp = array[0];
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (array[j] < array[i])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
//function for finding kth smallest element
int kthsmallest(int array[], int size, int k)
{
return array[k - 1];
}
int main()
{
int size;
cout << " enter the number of elements you want to enter : ";
cin >> size;
int* array=new int[size];
//populating the array
for (int i = 0; i < size; i++)
{
cout << "Enter the value of element " << i + 1 << " : ";
cin >> array[i];
}
//function call
sort_array(array, size);
int k = 0;
cout << " Enter the k'th number to find smallest element : ";
cin >> k;
while (k > size)
{
cout << " Error! given number is out of bound " << endl;
cout << " Enter again " << endl;
cin >> k;
}
//calling function
int smallest = 0;
smallest=kthsmallest(array, size, k);
cout << k<<"th smallest element is : " << smallest;
delete[]array;
array = NULL;
return 0;
}