Created
May 10, 2015 05:25
-
-
Save SumitJainUTD/4e5f7374237d33c64c36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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