-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTriInsertion3A.java
33 lines (29 loc) · 1.05 KB
/
TriInsertion3A.java
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
public class Insertion3A {
public static void main(String[] args) {
int [] tab = { -2, 78, 10, 245, 14, 4, 18, 19, 55, 21, 74, 2000, 154, 27, 12};
int comparaison = 0;
int affectation = 0;
int echange = 0;
int n = tab.length;
for(int i =0; i<n; i++) {
comparaison++;
int min = tab[i];
int j = i-1;
affectation+=2;
while(j>=0 && tab[j]>min) {
comparaison++;
tab[j+1] = tab[j];
affectation++;
echange++;
j--;
}
tab[j+1] = min;
affectation++;
}
for(int i =0; i<n; i++) {
System.out.println(tab[i]);
System.out.println();
}
System.out.println("Il y a eu " + comparaison + " comparaisons, " + affectation +" affectations et "+ echange + " echanges.");
}
}