排序- 希尔排序 ShellSort

本文详细介绍了一种希尔排序算法的具体实现过程,并通过实例演示了如何逐步完成数组从小到大的排序。该算法采用增量分组的方式进行排序,随着增量的减少,最终达到完全排序的目的。

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

package genius.sort;

import java.util.Arrays;

public class ShellSortFinal {
    public static void shellSortSmallToBig(int[] data) {
        int j = 0;
        int temp = 0;
        //外循环控制循环的次数,也就是分组希尔排序的次数....直到分组个数为1.。。
        System.out.println("the length of the array is "+data.length);
        for (int increment = data.length / 2; increment > 0; increment /= 2) {
            System.out.println("当分组数为increment:" + increment);
            for (int i = increment; i < data.length; i++) {
                temp = data[i];
                for (j = i - increment; j >= 0; j -= increment) {
                    /* System.out.println("j:" + j);
                     System.out.println("temp:" + temp);
                     System.out.println("data[" + j + "]:" + data[j]);*/
                    if (temp < data[j]) {
                        data[j + increment] = data[j];
                    } else {
                        break;
                    }
                }
                data[j + increment] = temp;
            }
            for (int i = 0; i < data.length; i++){
                System.out.print(data[i] + " ");
            }
            System.out.print("\n");
        }
    }

    public static void main(String[] args) {
        int[] data = new int[] { 26, 53, 67, 48, 57, 13, 48, 32, 60, 50 ,1};
        System.out.println(Arrays.toString(data));
        shellSortSmallToBig(data);
        System.out.println("ShellSortFinal.main() Excuted the final Result.......");
        System.out.println(Arrays.toString(data));
    }

}
[26, 53, 67, 48, 57, 13, 48, 32, 60, 50, 1]
the length of the array is 11
当分组数为increment:5
1 48 32 48 50 13 53 67 60 57 26 
当分组数为increment:2
1 13 26 48 32 48 50 57 53 67 60 
当分组数为increment:1
1 13 26 32 48 48 50 53 57 60 67 
ShellSortFinal.main() Excuted the final Result.......
[1, 13, 26, 32, 48, 48, 50, 53, 57, 60, 67]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值