-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
30 lines (25 loc) · 849 Bytes
/
Copy pathBubbleSort.java
File metadata and controls
30 lines (25 loc) · 849 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
26
27
28
29
30
import java.util.Arrays;
public class BubbleSort {
private static void sort(int[] array) {
for (int i = 0; i < array.length; i++) {
int swaps = 0;
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swaps = 1;
}
}
if (swaps == 0) {
break;
}
}
}
public static void main(String[] args) {
int[] array = {67, 44, 82, 17, 20};
System.out.println("Before sorting: " + Arrays.toString(array));
sort(array);
System.out.println("After sorting " + Arrays.toString(array));
}
}