-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
43 lines (37 loc) · 980 Bytes
/
Copy pathBubbleSort.java
File metadata and controls
43 lines (37 loc) · 980 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
package com.albert.algorithm.sort;
import java.util.Arrays;
/**
* 冒泡排序法 平均时间复杂度为O(n^2) 最好的时间复杂度为O(0) 稳定的
*
* @author Albert
* @see http://baike.baidu.com/view/254413.htm?fromId=1313793
*/
public class BubbleSort {
public static void main(String[] args) {
int[] array = {6, 5, 3, 9, 0, 2, 7, 6, 4, 12, 11};
sort(array);
}
/**
* 升序排列
*
* @param array
*/
public static void sort(int[] array) {
printArray(array);
int a = 0;
for (int i = 0; i < array.length - 1; i++) {
for (int j = 1; j < array.length - i; j++) {
if(array[j - 1] > array[j]) {
a = array[j - 1];
array[j - 1] = array[j];
array[j] = a;
}
}
printArray(array);
}
printArray(array);
}
private static void printArray(int[] array) {
System.out.print(Arrays.toString(array));
}
}