-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjetAlgo1.java
More file actions
176 lines (149 loc) · 5.13 KB
/
ProjetAlgo1.java
File metadata and controls
176 lines (149 loc) · 5.13 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.util.Arrays;
class ProjetAlgo1
{
//--------------- AFFICHER LE TABLEAU GRACE A ARRAY -----------------------//
static void AfficheTableau (int [] tab){
System.out.println(Arrays.toString(tab));
}
//--------------- CODE POUR LA TRI SELECTION -------------------//
static void TriSelect (int [] tab)
{
int comparaison = 0; //Lorsqu'il y a comparaison entre 2 valeurs
int echange = 0; //Lorsqu'il y a 3 affectations
int affectation =0; //Lorsqu'on attribut une valeur
int operation = 0; //Represente les comparaisons + affectations
int l = tab.length-1 ;
for ( int i = 0 ; i <= l ; i++)
{
comparaison +=1;
affectation +=1;
int m = i ;
for ( int j = i + 1 ; j <= l ; j++)
if ( tab[j] < tab[m] )
m = j ;
comparaison +=1;
affectation +=1;
int temp = tab[ m ];
tab[ m ] = tab[ i ];
tab[ i ] = temp;
affectation +=3;
echange +=1;
}
//-----------------------------AFFICHER-----------------------------------//
operation = comparaison + affectation;
System.out.println ("Il y a eu : " + affectation + " affections");
System.out.println ("Il y a eu : " + comparaison + " comparaisons");
System.out.println ("Il y a eu : " + echange + " echanges");
System.out.println ("Il y a eu : " + operation + " operations");
}
//---------------TRI tri_insertion------------------------//
public static void tri_insertion(int T[])
{
int comparaison1 = 0;
int affectation1 = 0;
int echange1 = 0;
int operation = 0;
int n = T.length;
for (int i = 1; i < n; i++)
{
comparaison1 ++;
int m = T[i];
int j = i-1;
affectation1 +=2;
while(j >= 0 && T[j] > m)
{
comparaison1 ++;
T[j+1] = T[j];
affectation1 ++;
echange1 ++;
j--;
}
T[j+1] = m;
affectation1 ++;
}
operation = comparaison1 + affectation1;
//---------------AFFICHER--------------------//
System.out.println ("Il y a eu : " + affectation1 + " affections");
System.out.println ("Il y a eu : " + comparaison1 + " comparaisons");
System.out.println ("Il y a eu : " + echange1 + " echanges");
System.out.println ("Il y a eu : " + operation + " operations");
}
//------------------------------TRI BULLE----------------------------------//
static void TriBulle (int table[])
{
int affectation = 0;
int comparaison = 0;
int echange = 0;
int operation = 0;
int l = table.length - 1 ;
for ( int i = l ; i >= 1 ; i -- ){
affectation += 1;
comparaison += 1;
for ( int j = 2 ; j <= i ; j ++ )
if ( table[j - 1] > table[j] )
{
int temp = table[j - 1] ;
table[j - 1] = table[j] ;
table[j] = temp ;
affectation += 2;
comparaison += 3;
echange += 1;
}
}
operation = comparaison + affectation;
//---------------AFFICHER--------------------//
System.out.println ("Il y a eu : " + affectation + " affections");
System.out.println ("Il y a eu : " + comparaison + " comparaisons");
System.out.println ("Il y a eu : " + echange + " echanges");
System.out.println ("Il y a eu : " + operation + " operations");
}
//==========================TRI BULLE OPTIMISE======================================//
static void TriBulleOpti (int table []){
boolean ordre = false;
int comparaison1=0;
int echange1 = 0;
int affectation1=0;
int operation = 0;
while (!ordre)
{
ordre = true;
comparaison1 += 1;
int l = table.length - 1 ;
for (int j = 0; j <l ; j++)
{
affectation1 += 1;
comparaison1 += 1;
if (table[j] > table[j + 1])
{
ordre = false;
int temp = table[j];
table[j] = table[j + 1];
table[j + 1] = temp;
affectation1 += 3;
comparaison1 += 1;
echange1 += 1;
}
}
}
operation = comparaison1 + affectation1;
//---------------AFFICHER--------------------//
System.out.println ("Il y a eu : " + affectation1 + " affections");
System.out.println ("Il y a eu : " + comparaison1 + " comparaisons");
System.out.println ("Il y a eu : " + echange1 + " echanges");
System.out.println ("Il y a eu : " + operation + " operations");
}
//--------------- RUN DU CODE ----------------//
public static void main ( String [] args )
{ //int [] tab = {1,1231,65,54,3,4124,421,5435,7657,223}; //VALEUR A METTRE DANS LE TABLEAU
int[] tab = {10,9,8,7,6,5,4,3,2,1};
//int[] tab = {1,2,3,4,5,6,7,8,9,10};
System.out.println ("Tableau sans le tri:");
AfficheTableau(tab);
//TriSelect (tab);
//tri_insertion (tab);
//TriBulle (tab);
TriBulleOpti (tab);
System.out.println ("Tableau trié :");
AfficheTableau (tab);
}
}