Skip to content

Commit a7282d2

Browse files
authored
Bubble sort fix (eugenp#2809)
1 parent 71de914 commit a7282d2

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

algorithms/src/main/java/com/baeldung/algorithms/bubblesort/BubbleSort.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ void bubbleSort(Integer[] arr) {
1919

2020
void optimizedBubbleSort(Integer[] arr) {
2121
int i = 0, n = arr.length;
22+
2223
boolean swapNeeded = true;
2324
while (i < n - 1 && swapNeeded) {
2425
swapNeeded = false;
25-
for (int j = i + 1; j < n - i; j++) {
26+
for (int j = 1; j < n - i; j++) {
2627
if (arr[j - 1] > arr[j]) {
28+
2729
int temp = arr[j - 1];
2830
arr[j - 1] = arr[j];
2931
arr[j] = temp;

0 commit comments

Comments
 (0)