1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public static void tri (String[] t) {
// index indiquant l'element le plus grand de list a partir d'une certaine position
int swapIndex;
String temp;
Collator frCollator = Collator.getInstance(new Locale("fr","FR"));
for(int k =0;k<t.length;k++) {
// on suppose que t[k] est l'element le plus grand de la list, donc on initialize swapIndex a -1. si sa valeur
// change dans la prochaine boocle, c'est qu'on a trouve un element plus grand et que ce dernier doit occuper
// la position 'k'
swapIndex = -1;
for (int i=k; i < t.length ; i++ ) {
if (frCollator.compare(t[i], t[k]) < 0)
swapIndex = i;
}
// on fait le swap
if (swapIndex != -1) {
temp = t[k];
t[k] = t[swapIndex];
t[swapIndex] = temp;
}
System.out.print(t[k] + " ");
}
} |
Partager