Skip to content

Commit 8881e9a

Browse files
make code more readable
1 parent ee0a48f commit 8881e9a

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

Sorts/BubbleSort.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@ class BubbleSort implements SortAlgorithm {
1818

1919
@Override
2020
public <T extends Comparable<T>> T[] sort(T array[]) {
21-
int last = array.length;
22-
//Sorting
23-
boolean swap;
24-
do {
25-
swap = false;
26-
for (int count = 0; count < last - 1; count++) {
27-
if (less(array[count], array[count + 1])) {
28-
swap = swap(array, count, count + 1);
29-
}
21+
for (int i = 0, size = array.length; i < size - 1; ++i) {
22+
boolean swapped = false;
23+
for (int j = 0; j < size - 1 - i; ++j) {
24+
swapped = less(array[j], array[j + 1]) && swap(array, j, j + 1);
3025
}
31-
last--;
32-
} while (swap);
26+
if (!swapped) {
27+
break;
28+
}
29+
}
3330
return array;
3431
}
3532

0 commit comments

Comments
 (0)