Skip to content

find one bug in bubbleSort #1164

@tangrenhui

Description

@tangrenhui

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions