Here is a complete implementation of the bubble sort algorithm in Java from Rosetta Code:
public static <E extends Comparable<? super E>> void bubbleSort(E[] comparable) {
boolean changed = false;
do {
changed = false;
for (int a = 0; a < comparable.length - 1; a++) {
if (comparable[a].compareTo(comparable[a + 1]) > 0) {
E tmp = comparable[a];
comparable[a] = comparable[a + 1];
comparable[a + 1] = tmp;
changed = true;
}
}
} while (changed);
}
Since you're probably doing this for a class, you should try to understand what is happening. Use lots of System.out.println()
statements to show how your program ran.
You should also find out how to use the debugger on your IDE. It will allow you to see how the program runs line by line, and how the data changes.