依次比较相邻的两个数。将小数放在前面,大数放在后面,
由于在排序过程中总是小数往前放,大数往后放,相当于气泡往上升,所以称作冒泡排序
代码
1
public
void
sort(
int
[] array)
2 {
3
4 int length = array.length;
5 for ( int i = 0 ;i < length - 1 ;i ++ )
6 {
7 for ( int j = length - 1 ;j > 1 ;j -- )
8 {
9
10 if (array[j] < array[i - 1 ])
11
12 {
13 int tmp = array[j];
14 array[j] = array[j - 1 ];
15 array[j - 1 ] = tmp;
16
17 }
18
19
20
21 }
22
23 }
24
25
26
27 }
2 {
3
4 int length = array.length;
5 for ( int i = 0 ;i < length - 1 ;i ++ )
6 {
7 for ( int j = length - 1 ;j > 1 ;j -- )
8 {
9
10 if (array[j] < array[i - 1 ])
11
12 {
13 int tmp = array[j];
14 array[j] = array[j - 1 ];
15 array[j - 1 ] = tmp;
16
17 }
18
19
20
21 }
22
23 }
24
25
26
27 }