exercice tri

This commit is contained in:
Rémi Heredero 2022-03-07 16:59:49 +01:00
parent f782acb78c
commit 4843b0aea5
2 changed files with 17 additions and 4 deletions

View File

@ -4,18 +4,31 @@ package C10_Tri_et_complexite.C101_Algorithmes;
public class Tri { public class Tri {
static int [] a = {4, 7, 1, 3, 6};
static void printList(int[] a){ static void printList(int[] a){
for (int i : a) { for (int i : a) {
System.out.print(i + " "); System.out.print(i + " ");
} }
System.out.println(); System.out.println();
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
int [] a = {4, 7, 1, 3, 6};
printList(a); printList(a);
int min = 0; for (int i = 0; i < a.length-1; i++) {
for (int i = min+1; i < a.length; i++) { int min = a[i];
int iMin = i;
for (int j = i; j < a.length; j++) {
if(a[j] < min){
min = a[j];
iMin = j;
}
}
int tmp = a[i];
a[i] = a[iMin];
a[iMin] = tmp;
}
printList(a);
} }
}
} }