Sorting algorithms/Bubble sort: Difference between revisions

m (→‎{{header|Java}}: Fixed it again)
Line 843:
=={{header|Java}}==
Bubble sorting (ascending) an array of any <tt>Comparable</tt> type:
<lang java>public static <E extends Comparable<? super E>> void bubbleSort(E[] comparable) {
<lang java> boolean changed = false;
do {
changeddo = false;{
changed = false;
for (int a = 0; a < comparable.length - 1; a++) {
iffor (comparable[int a].compareTo(comparable[ = 0; a +< 1])comparable.length >- 01; a++) {
E tmp =if (comparable[a];.compareTo(comparable[a + 1]) > 0) {
comparable[a] E tmp = comparable[a + 1];
comparable[a] = comparable[a + 1] = tmp;
changed comparable[a + 1] = truetmp;
changed = true;
}
}
} while (changed);
} while (changed);</lang>
 
For descending, simply switch the direction of comparison:
Anonymous user