-
Notifications
You must be signed in to change notification settings - Fork 20.8k
Closed
Description
I found one bug in bubble sort. the example unsort array is
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12}
the error function is
public <T extends Comparable> T[] sort(T array[]) {
for (int i = 0, size = array.length; i < size - 1; ++i) {
boolean swapped = false;
for (int j = 0; j < size -1 - i; ++j) {//Find the smallest one in each cycle
swapped = less(array[j], array[j + 1]) && swap(array, j, j + 1);
}
if (!swapped) {
break; //there is shouldn't use break to stop cycle
}
}
return array;
}
when use given function the result is
[78, 231, 54, 23, 12, 9, 6, 4, 1]
break should be replaced by continue
Abuubkar and 437865981
Metadata
Metadata
Assignees
Labels
No labels