public class BubbleSort {
public static void main(String[] args) {
int [] a = {18,62,68,82,65,9};
//排序前,先把内容打印出来
//可以发现一个规律
//后边界在收缩
//所以可以在外面套一层循环
for (int j = 0; j < a.length; j++) {
for (int i = 0; i < a.length-j-1; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
//把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
Java实现经典排序算法-冒泡排序
最新推荐文章于 2024-05-18 17:57:38 发布