Muito bom Felipe, mas vc esqueceu que este fórum eh de J2ME, ou seja,
não tem Comparator, Arrays…
Vinucho, aqui vai uma das técnicas que eu uso, eh uma das mais rápidas para
serem utilizadas no J2ME, eh o Shell Sort, é só adaptar para o que vc quer usar:
public class SortUtil {
public static void shellSort(int numbers[]) {
int i, j, increment, temp;
increment = 3;
while (increment > 0) {
for (i = 0; i < numbers.length; i++) {
j = i;
temp = numbers[i];
while ((j >= increment) && (numbers[j - increment] > temp)) {
numbers[j] = numbers[j - increment];
j = j - increment;
}
numbers[j] = temp;
}
if (increment / 2 != 0)
increment = increment / 2;
else if (increment == 1)
increment = 0;
else
increment = 1;
}
}
public static void shellSort(String numbers[], boolean invert) {
int i, j, increment;
String temp;
increment = 3;
while (increment > 0) {
for (i = 0; i < numbers.length; i++) {
j = i;
temp = numbers[i];
while ((j >= increment) &&
(
((!invert) && (numbers[j - increment].compareTo(temp) > 0)) ||
((invert) && (numbers[j - increment].compareTo(temp) < 0))
)
) {
numbers[j] = numbers[j - increment];
j = j - increment;
}
numbers[j] = temp;
}
if (increment / 2 != 0)
increment = increment / 2;
else if (increment == 1)
increment = 0;
else
increment = 1;
}
}
public static void shellSort(String values[][], int column, boolean invert) {
int i, j, increment;
String temp[];
increment = 3;
while (increment > 0) {
for (i = 0; i < values.length; i++) {
j = i;
temp = values[i];
while ((j >= increment) &&
(
((!invert) && (values[j - increment][column].compareTo(temp[column]) > 0)) ||
((invert) && (values[j - increment][column].compareTo(temp[column]) < 0))
)
) {
values[j] = values[j - increment];
j = j - increment;
}
values[j] = temp;
}
if (increment / 2 != 0)
increment = increment / 2;
else if (increment == 1)
increment = 0;
else
increment = 1;
}
}
public static void main(String args[]) {
SortUtil sortUtil = new SortUtil();
int[] array = new int[] {3, 4, 2, 5, 1, 0};
sortUtil.shellSort(array);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
System.out.println("-------------");
String[] ses = new String[] {"C", "D", "A", "B", "F", "E"};
sortUtil.shellSort(ses, false);
for (int i = 0; i < ses.length; i++) {
System.out.println(ses[i]);
}
System.out.println("-------------");
sortUtil.shellSort(ses, true);
for (int i = 0; i < ses.length; i++) {
System.out.println(ses[i]);
}
System.out.println("Teste do array bi-dimensional");
String[][] bis = new String[][] {
{"1", "C"},
{"2", "A"},
{"3", "D"},
{"4", "B"},
};
sortUtil.shellSort(bis, 1, false);
for (int i = 0; i < bis.length; i++) {
System.out.println(bis[i][0] + " - " + bis[i][1]);
}
System.out.println("Teste do array bi-dimensional invertido");
sortUtil.shellSort(bis, 1, true);
for (int i = 0; i < bis.length; i++) {
System.out.println(bis[i][0] + " - " + bis[i][1]);
}
}
}