Java bubble sort implementation.

int[] numbers = {3, 1, 9, 2, 4, 8, 6, 5, 7, 0};

System.out.println("before: " + Arrays.toString(numbers));
// output:
// before: [3, 1, 9, 2, 4, 8, 6, 5, 7, 0]
for (int i = 0; i < numbers.length; i++) {
    for (int j = 0; j < numbers.length - 1 - i; j++) {
        if (numbers[j] > numbers[j + 1]) {
            int tmp = numbers[j + 1];
            numbers[j + 1] = numbers[j];
            numbers[j] = tmp;
        }
    }
}
System.out.println("after: " + Arrays.toString(numbers));
// output:
// after: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
TOPâ–˛
Post tagged with: java, algorithm