冒泡排序算法

本文深入探讨了冒泡排序算法的实现,通过两种不同的方法展示了如何对整数数组进行排序。文章提供了详细的代码示例,包括交换元素和显示排序结果的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class Bubble {

    /*
    冒泡排序
     */


//交换
    private static void swap(int[] arry,int i,int j){
        int temp = arry[i];
        arry[i] = arry[j];
        arry[j] = temp;

    }


//显示
    private static void show(int[] arry){
        for (int i = 0; i < arry.length; i++) {
            System.out.println(arry[i]);
        }
    }


//
    private static void checkArray(int[] a){
        if (a==null || a.length < 2){
            return;
        }
    }
//冒泡排序算法1
    protected static void bubbleSort1(int[] a){
        checkArray(a);
        for (int i=0;i<a.length;i++){
            for (int j=0;j<a.length-i-1;j++) {
                if (a[j] > a[j + 1]) {
                    swap(a, j, j + 1);
                }
            }
        }

       show(a);
        }

 

//冒泡排序算法2
    protected static void bubbleSort2(int[] a){

        checkArray(a);
        for (int end = a.length-1; end > 0 ; end--) {
            for (int curr = 0;curr < end;curr++){
                if (a[curr] > a[curr+1]){
                    swap(a,curr,curr+1);
                }
            }
        }
        show(a);


    }

public static void main(String[] args){

int[] arry = {3,2,5,8,0,1,7};

bubbleSort1(arry);

bubbleSort2(arry);

 

}

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值