procédure triBulles(tableau T)
n ← longueur(T)
pour i de n-1 à 1 faire
pour j de 0 à i-1 faire
si T[j] > T[j+1] alors
échanger T[j] et T[j+1]
fin si
fin pour
fin pour
fin procédure
def bubble_sort(arr):
n = len(arr)
for i in range(n - 1, 0, -1):
for j in range(i):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
# Exemple d'utilisation
numbers = [64, 34, 25, 12, 22, 11, 90]
print(f"Avant le tri: {numbers}")
bubble_sort(numbers)
print(f"Après le tri: {numbers}")
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = n - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
// Échange des éléments
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] numbers = {64, 34, 25, 12, 22, 11, 90};
System.out.println("Avant le tri: " + Arrays.toString(numbers));
bubbleSort(numbers);
System.out.println("Après le tri: " + Arrays.toString(numbers));
}
}
#include
#include
void bubbleSort(std::vector& arr) {
int n = arr.size();
for (int i = n - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
// Échange des éléments
std::swap(arr[j], arr[j + 1]);
}
}
}
}
int main() {
std::vector numbers = {64, 34, 25, 12, 22, 11, 90};
std::cout << "Avant le tri: ";
for (int num : numbers) std::cout << num << " ";
std::cout << std::endl;
bubbleSort(numbers);
std::cout << "Après le tri: ";
for (int num : numbers) std::cout << num << " ";
std::cout << std::endl;
return 0;
}