forked from priyanka090700/DSA_Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthSmallestElement.java
More file actions
58 lines (49 loc) · 1.27 KB
/
Copy pathKthSmallestElement.java
File metadata and controls
58 lines (49 loc) · 1.27 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
// Ques 3.Given an array arr[] and a number K where K is smaller than size of array, the task is to find the Kth smallest element in the given array.
import java.util.Scanner;
public class KthSmallestElement {
static void sort(int arr[],int size,int k) {
for(int i=size/2-1;i>=0;i--) {
heapify(arr,size,i);
}
for(int i=size-1;i>=size-k;i--) {
int temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
heapify(arr,i,0);
}
}
static void heapify(int arr[],int size,int i) {
int small=i;
int l=2*i+1;
int r=2*i+2;
if(l<size && arr[l] <arr[small]) {
small=l;
}
if(r<size && arr[r]<arr[small]) {
small=r;
}
if(small!=i) {
int temp=arr[i];
arr[i]=arr[small];
arr[small]=temp;
heapify(arr,size,small);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//Given an array arr[] and a number K where K is smaller than size of array.
//The task is to find the Kth smallest element in the given array.
//It is given that all array elements are distinct.
Scanner s=new Scanner(System.in);
int n,k;
n=s.nextInt();
int [] arr= new int[n];
for (int i = 0; i < n; i++) {
arr[i]=s.nextInt();
}
k=s.nextInt();
sort(arr,n,k);
System.out.println("kth smallest element is "+arr[n-k]);
s.close();
}
}