-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
41 lines (32 loc) · 929 Bytes
/
BubbleSort.java
File metadata and controls
41 lines (32 loc) · 929 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
31
32
33
34
35
36
37
38
39
40
41
package test;
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {6, 3, 2, 7, 1, 5, 0, 9, 8, 4};
// int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
// int[] arr = new int[1000];
// for(int i = 0 ; i < arr.length ; i++) {
// arr[i] = (int)(Math.random() * 101);
// }
System.out.println(Arrays.toString(arr));
System.out.println();
for(int i = 1 ; i < arr.length ; i++) {
boolean swapped = false;
for(int j = 0 ; j < arr.length - i ; j++) {
if(arr[j] > arr[j + 1]) {
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
swapped = true;
}
}
if(!swapped) {
break;
}
System.out.println(i + " : " + Arrays.toString(arr));
}
System.out.println();
System.out.println(Arrays.toString(arr));
}
}