-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbling.java
More file actions
58 lines (54 loc) · 1.39 KB
/
bubbling.java
File metadata and controls
58 lines (54 loc) · 1.39 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package ds;
/**
* 不要被那些所谓的边界值吓到,要分清事实
* @author fqx
*
*/
public class bubbling {
//升序排列
public void BubberSort(int [] a){
for (int i = 0; i < a.length - 1; i++) {
//每两个数比较 都是 右 <= 左;
//一轮结束后,就有一个数的位置固定了(在高位)
//所以一轮比较结束后,剩余的未比较的长度是变化的如下循环条件
for (int j = 0; j < a.length - 1 - i; j++) {
if(a[j + 1] > a[j]){
int tmp = a[j];
a[j] = a[j+1];
a[j + 1] = tmp;
}
}
show(a);
}
}
public void bubble_1(int [] a){
int i = a.length - 1;
while(i > 0){
//j 最大只能取到 len-2:因为 a[j] 要与a[j+1]比较
int pos = 0;
for (int j = 0; j < i; j++) {
if(a[j] > a[j+1]){
pos = j;
int tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
i = pos;
System.out.println("i " + i);
show(a);
}
}
public void show(int [] a){
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
bubbling b = new bubbling();
int [] a = {3,0,4,2,6,9,5,10,1};
b.BubberSort(a);
b.show(a);
}
}