Skip to content

Instantly share code, notes, and snippets.

@SumitJainUTD
Created May 10, 2015 05:25
Show Gist options
  • Save SumitJainUTD/4e5f7374237d33c64c36 to your computer and use it in GitHub Desktop.
Save SumitJainUTD/4e5f7374237d33c64c36 to your computer and use it in GitHub Desktop.
import java.util.PriorityQueue;
public class KthSmallestElementInArray {
public static int find(int [] A, int k){
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(int i=0;i<A.length;i++){
pq.offer(A[i]);
}
int n = -1;
while(k>0){
n = pq.poll();
k--;
}
return n;
}
public static void main(String[] args) {
int[] A = { 1, 2, 10, 20, 40, 32, 44, 51, 6 };
int k = 4;
System.out.println("4th smallest element:" + find(A,4));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment