-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaoPaoSort.java
More file actions
31 lines (29 loc) · 996 Bytes
/
MaoPaoSort.java
File metadata and controls
31 lines (29 loc) · 996 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
package algorithmBase.sort;
/**
* 冒泡排序 时间复杂度 O(N^2)
*/
public class MaoPaoSort {
public static void maoPaoSort(int[] arr) {
// 记录最后一次交换位置
int lastExchangeIndex = 0;
// 无序数列的边界,每次比较只需要比较到这里
int sortBorder = arr.length - 1;
for (int i = 0; i < arr.length; i++) {
// 有序标记,每一轮的初始值都是true
boolean isSorted = true;
for (int j = 0; j < sortBorder; j++) {
int tmp = 0;
if (arr[j] < arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
isSorted = false;
// 更新为最后一次交换元素的位置
lastExchangeIndex = j;
}
}
sortBorder = lastExchangeIndex;
if (isSorted) break;
}
}
}