Created
May 27, 2018 06:39
-
-
Save thmain/e1879523f97b631f877a8ef041666527 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