首页 > IT职业 > IT职场 > 排序算法之——冒泡排序算法
public class BubbleSort {
public static void main(String[] args) {
int[] sort ={1,2,3,4,5,6,7,8,9,0,11,22,33,44,55,66,77,88,99,00};
int out,in;
//外循环,确定循环次数为sort.length次,这样可以保证每一个数的位置正确
for(out = 0; out sort[in + 1]) {
//定义一个中间变量并将sort[in]赋给它
int temp = sort[in];
//将sort[in+1]的值赋给sort[in]
sort[in] = sort[in+1];
//把前面的temp(也就是原来的sort[in])赋给sort[in+1]
sort [in + 1] = temp;
//当此判断结束,sort[in]和sort[in+1]互换位置
}
//当此循环完了之后,相邻两个数中的大的将在右边
}
//经过了sort.length次循环,排序已经完成
}
for(int i : sort) {
System.out.print(i + " ");
}
}
}
结果:0 0 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2799/viewspace-2799002/ ,如需转载,请注明出处,否则将追究法律责任。