-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
44 lines (41 loc) · 1.2 KB
/
Copy pathBubbleSort.java
File metadata and controls
44 lines (41 loc) · 1.2 KB
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
42
43
44
package core;
/**
* @author luozhou
* @Description:冒泡排序算法
* @date 2018/10/29 13:41
*/
public class BubbleSort {
/**
* 基本冒泡排序算法*/
public static void sortByBubble(int[] needSort){
int tmp;
for (int i = 0; i <needSort.length-1 ; i++) {
for (int j = 1; j <needSort.length ; j++) {
if (needSort[j-1]>needSort[j]){
tmp=needSort[j-1];
needSort[j-1]=needSort[j];
needSort[j]=tmp;
}
}
}
}
/**
* 改进版冒泡排序算法
* 改进思路:排序过程有可能已经有序了,后面可以避免无谓的循环了
* **/
public static void sortByBubbleByFlag(int[] needSort){
int tmp;
for (int i = 0; i <needSort.length-1 ; i++) {
boolean flag=false;
for (int j = 1; j <needSort.length ; j++) {
if (needSort[j-1]>needSort[j]){
tmp=needSort[j-1];
needSort[j-1]=needSort[j];
needSort[j]=tmp;
flag=true;
}
}
if (!flag)break;
}
}
}