Merge pull request #1174 from cganey/BubbleSortLogic

Update BubbleSort.java
This commit is contained in:
Yang Libin 2019-12-16 20:49:10 +08:00 committed by GitHub
commit d9c7b8477e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,7 +21,10 @@ class BubbleSort implements SortAlgorithm {
for (int i = 0, size = array.length; i < size - 1; ++i) { for (int i = 0, size = array.length; i < size - 1; ++i) {
boolean swapped = false; boolean swapped = false;
for (int j = 0; j < size - 1 - i; ++j) { for (int j = 0; j < size - 1 - i; ++j) {
swapped = less(array[j], array[j + 1]) && swap(array, j, j + 1); if (less(array[j], array[j + 1])) {
swap(array, j, j + 1);
swapped = true;
}
} }
if (!swapped) { if (!swapped) {
break; break;