原理
重复比较相邻元素,择大者互换,从而完成排序
开始排序
第一趟
length-1趟
代码实现
@Test
public void Bubblesort() {
int[] data= {1, 4, 3, 2, 7, 6, 5, 8, 9, 0};
int position , scan,temp;
for (position=data.length-1; position >=0 ; position--) {
for (scan =0;scan<=position-1;scan++) {
if (data[scan]>(data[scan+1]))
{
temp=data[scan];
data[scan]=data[scan+1];
data[scan+1]=temp;
}
}
}
for (int a:data) System.out.println(a);
性能分析
平均时间复杂度: O(n^2)
稳定的