JAVA中的经典算法之冒泡排序(BUBBLE SORT)

原理:比较两个相邻的元素,将值大的元素交换至右端(或者是将小值交换至右端)

逻辑思路参考:https://www.cnblogs.com/shen-hua/p/5422676.html

public class Bbble {

    public void BubbleSort(int[] arry) {
        for (int i = 0; i < arry.length; i++) {//外层循环控制排序趟数
            for (int j = 0; j < arry.length - i - 1; j++) {//内层循环控制每一趟排序多少次
                if (arry[j] > arry[j + 1]) {//'>'为升序排列,'<'为降序排列
                    int temp = arry[j];
                    arry[j] = arry[j + 1];
                    arry[j + 1] = temp;
                }
            }
        }
        //用集合的方式将数组打印出来
        List list=new ArrayList();
        for (int arr : arry) {
            list.add(arr);
            System.out.println("升序打印出的数组为"+arr);//此方式在于循环打印
        }
        System.out.println("集合升序打印出的数组为"+list);
    }




public class BubbleSort {

    public static void main(String[] args) {
        int[] arrys = {4, 7, 9, 10, 3, 5, 89, 0};
        Bbble bbble = new Bbble();
        bbble.BubbleSort(arrys);
    }

}


运行结果

升序打印出的数组为89
升序打印出的数组为10
升序打印出的数组为9
升序打印出的数组为7
升序打印出的数组为5
升序打印出的数组为4
升序打印出的数组为3
升序打印出的数组为0
集合方式升序打印出的数组为[89, 10, 9, 7, 5, 4, 3, 0]

 

转载于:https://www.cnblogs.com/lindaiyu/p/10909566.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值