-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.java
More file actions
25 lines (23 loc) · 799 Bytes
/
bubbleSort.java
File metadata and controls
25 lines (23 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Arrays;
public class bubbleSort {
public static void main(String[] args) {
int[] arr = {7, 6, 5, 4, 3};
bubble(arr);
System.out.println(Arrays.toString(arr));
}
static void bubble(int[] arr) {
int length = arr.length;
for (int i = 0; i < length - 1; i++) {
boolean swapped = false; // Reset swapped to false at the beginning of each pass
for (int j = 1; j < length - i; j++) {
if (arr[j - 1] > arr[j]) {
int temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
swapped = true;
}
}
if (!swapped) break; // If no swaps were made, the array is sorted
}
}
}