冒泡排序法、选择排序法、插入排序法、希尔排序法

该博客详细展示了四种常见的排序算法:冒泡排序、选择排序、插入排序和希尔排序的Java实现。通过代码实例,解释了每种排序算法的工作原理和步骤,有助于理解数据结构与算法的基础知识。

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

 //冒泡排序法
public class MyDemo4{

    public static void main(String [] args){
        int [] scores = {8,4,2,1,23,344,12};
        for ( int row=0;row<scores.length-1;row++) {
            
            for ( int col=0;col<scores.length-1-row;col++ ) {
                
                if (scores[col]<scores[col+1]) {
                    int temp = scores[col];
                    scores[col] = scores[col+1];
                    scores[col+1] = temp;
                }
            }
        }
        for (int i=0;i<scores.length;i++){
            System.out.println(scores[i]+"\t");
        }
        
    }
}
    


    
        //选择排序法

public class MyDemo4{

    public static void main(String [] args){
        int [] scores = {8,4,2,1,23,344,12};

        for ( int row=0;row<scores.length-1;row++) {
            int last = scores.length-1-row;  // 最后坐标
            int max = 0;//初始时默认第一个是最高的
            //第一个数与第二个数开始比较
            for ( int col=1;col<scores.length-row;col++) {
                //如果第一个数小于第二个数,将第二个数的值赋给第一个数
                if ( scores[max] < scores[col] ) {
                    max = col;
                }
            }
            //一轮结束,两数位置交换
            //如果最大值的坐标不等于最后一个值的坐标则进行交换
            if ( max!=last) {
                //数组中最大值与数组最后一个值的位置进行互换
                //定义一个空容器,将最大值的坐标赋给容器
                int temp=scores[max];
                //将最后一个数值的坐标赋值给最大值的坐标
                scores[max] = scores[last];
                //将temp中的坐标赋值给最后一个数值的坐标
                scores[last] = temp;
            }
        }
        for (int i=0;i<scores.length;i++){
            System.out.println(scores[i]+"\t");
        }
        
    }
}


/ 插入排序法

public class MyDemo1{

    public static void main(String [] args){
    
        int [] scores = {8,4,2,1,23,344,12};
        
        for ( int i=0;i<scores.length-1;i++ ) {
            int curr = scores[i+1];//获取要进行判断的数字
            for ( int j=i;j>=0;j--) {
                //如果当前值小于比较的数
                if ( curr<scores[j]) {
                    //将比较的数向后移一位
                    scores[j+1] = scores[j];
                }
                //否则退出循环
                else {
                    break;
                }
                //退出循环后,将当前值写到后移一位数值的原位置
                scores[j] = curr;
            }
        }
        for ( int i=0;i<scores.length;i++) {
            System.out.print(scores[i]+"\t");
        }
    }
}


//希尔排序法

public class MyDemo2{

    public static void main(String [] args){
    
        int [] scores = {8,4,2,1,23,344,12};
        //分组插入
        //首先计算步长 scores.length/2;  步长为3:[8、1、12] 、 [4、23] 、 [2、344]
        //小组排序[1、8、12]、[4、23]、[2、344]====>[1、4、2、8、23、344、12]
        //重新计算新步长  原步长/2     1===>
        // 计算步长 每次步长缩短一半  直到步长为一  就结束
        for ( int jump=scores.length/2;jump>0;jump/=2) {
            //从每组第2个数开始(多少组取决于步长)
            for ( int i=jump;i<scores.length;i++) {
                //取出每组第2个数
                int value = scores[i];
                //循环  小组中数据排序
                int j;
                for ( j=i-jump;j>=0 && scores[j]>value;j-=jump) {
                    scores[j+jump] = scores[j];
                }
                scores[j+jump] = value;
            }
        }
        for ( int i=0;i<scores.length;i++) {
            System.out.print(scores[i]+"\t");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值