Skip to content

Instantly share code, notes, and snippets.

@bchetty
Last active March 25, 2016 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bchetty/95b3dfde692090392d22 to your computer and use it in GitHub Desktop.
Save bchetty/95b3dfde692090392d22 to your computer and use it in GitHub Desktop.
PQTest.java
import java.util.Comparator;
import java.util.PriorityQueue;
/**
*
* @author Babji P, Chetty
*/
public class PQTest {
public static void main(String[] args) {
PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>(10, new Comparator<Integer>() {
public int compare(Integer int1, Integer int2) {
boolean flag1 = isPrime(int1);
boolean flag2 = isPrime(int2);
if (flag1 == flag2){
return int1.compareTo(int2);
} else if (flag1) {
return -1;
} else if(flag2) {
return 1;
}
return 0;
}
});
pQueue.add(10);
pQueue.add(8);
pQueue.add(6);
pQueue.add(4);
pQueue.add(2);
pQueue.add(9);
pQueue.add(7);
pQueue.add(5);
pQueue.add(3);
pQueue.add(1);
while(true) {
Integer head = pQueue.poll();
if(head == null) {
break;
}
System.out.print(head + " <-- ");
}
}
/**
*
* @param n
* @return
*/
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
if (n == 2) {
return true;
}
if (n % 2 == 0) {
return false;
}
long m = (long) Math.sqrt(n);
for (long i = 3; i <= m; i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment