-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkth_smallest.cpp
57 lines (46 loc) · 973 Bytes
/
kth_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
57
#include<iostream>
using namespace std;
void Heapify(int arr[],int n,int i){
int left = 2*i+1;
int right = 2*i+2;
int largest = i;
if(left<n && arr[largest]<arr[left]){
left = largest;
}
if(right<n && arr[largest]>arr[right]){
right = largest;
}
if(largest!=i){
swap(arr[largest],arr[i]);
Heapify(arr,n,largest);
}
}
void BuildHeap(int arr[],int n){
for(int i=n/2 -1;i>=0;i--){
Heapify(arr,n,i);
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
}
int main(){
int n;
cout<<"Enter the number of array"<<endl;
cin>>n;
cout<<"Enter the elements of array"<<endl;
int *arr=new int[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
printArray(arr,n);
int x;
cout<<"ENter your Kth smallest NUmber "<<endl;
cin>>x;
BuildHeap(arr,x);
for(int i=x;i<n;i++){
if(arr[0]>arr)
}
}