经典冒泡排序算法

public static void main(String[] args) {
    int[] a = {23, 4, 5, 1, 3, 33, 89};
    for (int i = 0; i < a.length; i++) {
        //外层循环控制循环的次数
        for (int j = 0; j < a.length - 1 - i; j++) {//内层循环比对数据,交换数据的位置
            if(a[j] > a[j+1]){
                int temp = a[j];//临时变量
                //交换数据位置
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }
    //输出排序后
    for (int i = 0; i < a.length; i++) {
        System.out.println(a[i]);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37461349/article/details/84938252
今日推荐