Skip to content

Commit 33a3daa

Browse files
authored
Merge pull request #2 from Feature/Tri-a-bulles-optimise
Feature/tri a bulles optimise
2 parents 5bca98c + b99df82 commit 33a3daa

3 files changed

Lines changed: 73 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
*.class
22
.idea
3-
untitled104.iml
3+
untitled104.iml
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Tri à bulles optimisé
2+
FONCTION ENTIER TriBulle(tableau: t)
3+
// Définir les variables
4+
VARIABLE
5+
INT: n
6+
BOOLEAN: echange
7+
// Début de l'algorithme
8+
DEBUT
9+
// Initialisation
10+
n <- TAILLE(tableau)
11+
echange <- VRAI
12+
temp
13+
14+
// Tant qu'il y a echange
15+
TANT QUE echange EST VRAI FAIRE
16+
// Initialisation
17+
echange <- FAUX
18+
19+
// On parcourt le tableau
20+
POUR i DE 1 A n FAIRE
21+
// On compare les éléments suivants
22+
SI tableau[i] > tableau[i+1] ALORS
23+
// On échange les valeurs
24+
temp <- tableau[i]
25+
tableau[i] <- tableau[i+1];
26+
tableau[i+1] <- temp;
27+
echange <- VRAI
28+
FIN SI
29+
FIN POUR
30+
FIN TANT QUE
31+
RETOURNE tableau
32+
FIN
33+
FIN FONCTION
34+
// al
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Import Arrays
2+
import org.jetbrains.annotations.NotNull;
3+
4+
import java.util.Arrays;
5+
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
// write your code here
10+
// Clear console
11+
System.out.print("\033[H\033[2J");
12+
13+
// Tableau de test
14+
int[] tableau = { 1, 3, 7, 2, 6, 4, 5, 8, 10, 9 };
15+
16+
System.out.println("Tableau de test : "+Arrays.toString(triBulle(tableau)));
17+
18+
}
19+
20+
public static int[] triBulle(int @NotNull [] tableau) {
21+
int n = tableau.length;
22+
boolean echange = true;
23+
while (echange) {
24+
echange = false;
25+
for (int i = 0; i < n - 1; i++) {
26+
if (tableau[i] > tableau[i + 1]) {
27+
int temp = tableau[i];
28+
tableau[i] = tableau[i + 1];
29+
tableau[i + 1] = temp;
30+
echange = true;
31+
}
32+
}
33+
}
34+
35+
// Return le resultat
36+
return tableau;
37+
}
38+
}

0 commit comments

Comments
 (0)