mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
@ -18,18 +18,15 @@ class BubbleSort implements SortAlgorithm {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Comparable<T>> T[] sort(T array[]) {
|
public <T extends Comparable<T>> T[] sort(T array[]) {
|
||||||
int last = array.length;
|
for (int i = 0, size = array.length; i < size - 1; ++i) {
|
||||||
//Sorting
|
boolean swapped = false;
|
||||||
boolean swap;
|
for (int j = 0; j < size - 1 - i; ++j) {
|
||||||
do {
|
swapped = less(array[j], array[j + 1]) && swap(array, j, j + 1);
|
||||||
swap = false;
|
}
|
||||||
for (int count = 0; count < last - 1; count++) {
|
if (!swapped) {
|
||||||
if (less(array[count], array[count + 1])) {
|
break;
|
||||||
swap = swap(array, count, count + 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
last--;
|
|
||||||
} while (swap);
|
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user