Implementation in Java

suggest change
import java.util.Arrays;

public class BubbleSort {
    public static void sort(int[] values) {
        for (int end = values.length - 1; end > 0; end--) {
            boolean swapped = false;
            for (int index = 0; index < end; index++) {
                if (values[index] > values[index + 1]) {
                    int temporary = values[index];
                    values[index] = values[index + 1];
                    values[index + 1] = temporary;
                    swapped = true;
                }
            }
            if (!swapped) return;
        }
    }

    public static void main(String[] args) {
        int[] values = {4, 2, 9, 6, 23, 12, 34, 0, 1};
        sort(values);
        System.out.println(Arrays.toString(values));
    }
}
[0, 1, 2, 4, 6, 9, 12, 23, 34]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents