-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
36 lines (32 loc) · 968 Bytes
/
BubbleSort.java
File metadata and controls
36 lines (32 loc) · 968 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
package algorithms.sort;
import structires.collections.Collections;
/**
* Created : zzc
* Time : 2017/9/25
* Email : zzcm159@gmail.com
* Description :冒泡排序
*/
public class BubbleSort {
public static void main(String arg[]) {
int[] arr = new int[]{10, 2, 5, 3, 20, 12, 23, 22, 14};
int[] arrNew = bubble(arr);
for (int i = 0; i < arrNew.length; i++) {
System.out.println(arrNew[i]);
}
}
private static int[] bubble(int[] arr) {
if (arr != null && (arr.length > 1)) {
int size = arr.length;
for (int i = 1; i < size; i++) {
for (int j = 0; j < size - i; j++) {
if (arr[j] > arr[j + 1]) {
arr[j] += arr[j + 1];
arr[j + 1] = arr[j] - arr[j + 1];
arr[j] -= arr[j + 1];
}
}
}
}
return arr;
}
}