We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 211c28b commit 28eafdeCopy full SHA for 28eafde
src/main/java/com/thealgorithms/sorting/BubbleSort.java
@@ -1,16 +1,21 @@
1
package com.thealgorithms.sorting;
2
-
3
public class BubbleSort {
+
4
public static void bubbleSort(int[] arr) {
5
int n = arr.length;
6
+ boolean swapped;
7
for (int i = 0; i < n - 1; i++) {
- for (int j = 0; j < n - i - 1; j++) {
8
+ swapped = false;
9
+ for (int j = 0; j < n - 1 - i; j++) {
10
if (arr[j] > arr[j + 1]) {
11
+ // swap
12
int temp = arr[j];
13
arr[j] = arr[j + 1];
14
arr[j + 1] = temp;
15
+ swapped = true;
16
}
17
18
+ if (!swapped) break;
19
20
21
0 commit comments