
Bubble sort in Java
For anyone looking for a Java bubble sort :O)
import java.util.Arrays;
public class BubbleSort {
BubbleSort() {
int[] unsorted = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
for (int outer = 0; outer < unsorted.length; outer++) {
for (int inner = 0; inner < unsorted.length; inner++) {
if (inner < unsorted.length - 1) {
if (unsorted[inner] > unsorted[inner + 1]) {
// store counter + 1 in a tmp variable and swap
int tmp = unsorted[inner + 1];
// swap
unsorted[inner + 1] = unsorted[inner];
unsorted[inner] = tmp;
}
}
}
}
System.out.print(Arrays.toString(unsorted));
}
public static void main(String[] args) {
new BubbleSort();
}
}