I came across a problem such that:
WAP to sort prime numbers smaller than given N by digits. If N is 40,
the output should be 11, 13, 17, 19, 2, 23, 29, 3, 31, 37, 39, 5, 7.
Note: Limit memory use.
Getting primary number was the easy. But I could not figure out an efficient way of lexicographic sorting of array of integer.
public static void getPrimeNumbers(int limit) {
for (int i=2; i<=limit; i++) {
if(isPrime(i)) {
System.out.println(i);
}
}
}
public static boolean isPrime(int number) {
for(int j=2; j<number; j++) {
if(number%j==0) {
return false;
}
}
return true;
}
public static void lexographicSorting() {
int[] input = {2,3,5,7,11,13,17,19};
int[] output = {};
for (int i=0; i<input.length; i++) {
for(int j=0; j<input.length; j++) {
////Stuck at this part.
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…