forked from nibnait/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_Bubble.java
More file actions
45 lines (36 loc) · 976 Bytes
/
a_Bubble.java
File metadata and controls
45 lines (36 loc) · 976 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
41
42
43
44
45
package Algorithms_4thEdition.a_Sorting;
import Standard.std;
/**
* Bubble_Sort
* 时间复杂度:O(n^2)
*
*【思路】
* 共遍历n次数组A[],(i=[0,n-1])
* 每次比较 A[j]和A[j+1], 每遍历一次选出一个最大值 放在数组A[]的倒数第i个位置上
* 【
* j的范围可以优化 、 [0,n-1-i]
* 】
*
* Created by nibnait on 2016/9/23.
*/
public class a_Bubble {
public static void main(String[] args) {
int[] A = new int[]{54,35,48,36,27,12,44,44,8,14,26,17,28};
int[] B = new int[13];
A = Bubble_Sort(A, 13);
for (int i = 0; i < A.length; i++) {
System.out.print(A[i]+" ");
}
}
public static int[] Bubble_Sort(int[] a, int n) {
int temp;
for(int i=0; i<n-1; i++){
for(int j=0; j<n-1-i; j++){
if(a[j]>a[j+1]){
std.swap(a, j, j+1);
}
}
}
return a;
}
}