Skip to content

Commit 3259944

Browse files
authored
Update BubbleSort.java
Output from print(integers) returns [78, 231, 54, 23, 12, 9, 6, 4, 1] Correct output should be: [231, 78, 54, 23, 12, 9, 6, 4, 1]
1 parent e08408c commit 3259944

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Sorts/BubbleSort.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ public <T extends Comparable<T>> T[] sort(T array[]) {
2121
for (int i = 0, size = array.length; i < size - 1; ++i) {
2222
boolean swapped = false;
2323
for (int j = 0; j < size - 1 - i; ++j) {
24-
swapped = less(array[j], array[j + 1]) && swap(array, j, j + 1);
24+
if (less(array[j], array[j + 1])) {
25+
swap(array, j, j + 1);
26+
swapped = true;
27+
}
2528
}
2629
if (!swapped) {
2730
break;

0 commit comments

Comments
 (0)