Implementation in Java
suggest changeimport 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]
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents