-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
37 lines (31 loc) · 950 Bytes
/
BubbleSort.java
File metadata and controls
37 lines (31 loc) · 950 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
package SortPractice;
/**
* @auther: wilson
* @version: v1.0 创建时间: 2019-12-16
*
*/
import java.util.Arrays;
/**
* 思路:每次内循环把最大的数冒泡到最右边
* 其中的flag用来优化,当一轮下来如果没有发生交换则代表已经排好序直接退出循环
*/
public class BubbleSort {
public static Integer[] bubbleSort(Integer[] dataSources) {
Integer temp;
for(int i = 1; i < dataSources.length ; i++){
boolean flag = true;
for (int j = 0; j < dataSources.length -i ; j++){
if (dataSources[j] > dataSources[j+1]){
temp = dataSources[j+1];
dataSources[j+1] = dataSources[j];
dataSources[j] = temp;
flag = false;
}
}
if (flag) {
break;
}
}
return dataSources;
}
}