Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/main/java/com/thealgorithms/sorts/BubbleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,26 @@
* @return the sorted array.
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
for (int i = 1, size = array.length; i < size; ++i) {
boolean swapped = false;
for (int j = 0; j < size - i; ++j) {
if (SortUtils.greater(array[j], array[j + 1])) {
SortUtils.swap(array, j, j + 1);
swapped = true;
}
}
if (!swapped) {
break;
public <T extends Comparable<T>> T[] sort(T[] array) {
for (int i = 1, size = array.length; i < size; ++i) {
boolean swapped = false;

for (int j = 0; j < size - i; ++j) {
if (SortUtils.greater(array[j], array[j + 1])) {

// Swap using a temporary variable
T temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;

swapped = true;
}
}
return array;

if (!swapped) {
break;
}
}

return array;
}

Check failure on line 47 in src/main/java/com/thealgorithms/sorts/BubbleSort.java

View workflow job for this annotation

GitHub Actions / AnalyzeJava

reached end of file while parsing

Check failure on line 47 in src/main/java/com/thealgorithms/sorts/BubbleSort.java

View workflow job for this annotation

GitHub Actions / AnalyzeJava

reached end of file while parsing

Check failure on line 47 in src/main/java/com/thealgorithms/sorts/BubbleSort.java

View workflow job for this annotation

GitHub Actions / build

reached end of file while parsing

Check failure on line 47 in src/main/java/com/thealgorithms/sorts/BubbleSort.java

View workflow job for this annotation

GitHub Actions / build

reached end of file while parsing
Loading