-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
40 lines (37 loc) · 912 Bytes
/
BubbleSort.java
File metadata and controls
40 lines (37 loc) · 912 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
package sort;
/**
* @author ahscuml
* @date 2018/7/17
* @time 10:39
*/
public class BubbleSort {
private BubbleSort() {
}
/**
* 冒泡排序算法
*/
public static void sort(Comparable[] arr) {
int n = arr.length;
// 对冒泡排序的优化
int newn;
do {
newn = 0;
for (int i = 1; i < n; i++) {
if (arr[i - 1].compareTo(arr[i]) > 0) {
swap(arr, i - 1, i);
// 可以记录最后一次的交换位置,在此之后的元素在下一轮扫描中均不考虑
newn = i;
}
n = newn;
}
} while (newn > 0);
}
/**
* 交换函数
*/
private static void swap(Comparable[] arr, int i, int j) {
Comparable temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}