希尔排序java实现

希尔排序

希尔排序的思想是使数组中任意间隔为h的元素都是有序的

希尔排序轨迹:间隔为 5,3, 1

排序前

4938659776132749554

第一次间隔为5,得到的子序列为:

(49, 13) (38, 27) (65, 49) (97, 55) (76, 4)

对子序列进行插入排序,得到:

1327495544938659776

第二次间隔为3,得到的子序列为:

(13, 55, 38, 76) (27, 4, 65) (49, 49, 97)

对子序列进程插入排序,得到:

1344938274955659776

最后一次间隔为1,得到的子序列就是第二次排序后的序列,对该序列进行插入排序

4132738494955657697

代码实现

回顾一下插入排序的代码实现

插入排序

遍历数组,将数据插入到合适到位置
第一次:只有一个数,那么它一定在它最合适的位置
第二次:比较第二个数与第一个数的大小,如果第二个数小于第一个数,交换他们位置
第三次:比较第三个数与前两个数的大小,并按照比较结果交换它们位置
直到数组遍历完

package com.wy.mycode.algorithm.sort;

import com.wy.mycode.algorithm.sort.common.SortExample;
import com.wy.mycode.algorithm.sort.common.TestExampleData;

/**
 * @author HelloWorld
 * @create 2022/6/3 21:11
 * @email helloworld.dng@gmail.com
 * 插入排序
 */
public class Insertion extends SortExample {
    /**
     * @description 遍历数组,将数据插入到合适到位置
     *              第一次:只有一个数,那么它一定在它最合适的位置
     *              第二次:比较第二个数与第一个数的大小,如果第二个数小于第一个数,交换他们位置
     *              第三次:比较第三个数与前两个数的大小,并按照比较结果交换它们位置
     *              直到数组遍历完
     * @author HelloWorld
     * @create 2022/6/3 21:13
     * @param array
     * @return void
     */
    public static void sort(Comparable[] array) {
        for (int i = 1; i < array.length; i++) {
            for (int j = i; j > 0 && less(array[j], array[j-1]); j--) {
                exch(array, j, j-1);
            }
        }
    }

    public static void main(String[] args) {
        TestExampleData[] testData = TestExampleData.getTestData(10);
        show("排序前",  testData);
        sort(testData);
        if (isSorted(testData)) {
            show("排序后", testData);
            return;
        }
        System.out.println("排序失败");
    }
}

而希尔排序中最后一步一定是对整个序列进行插入排序,故可以参照插入排序的实现逻辑

希尔排序java实现
package com.wy.mycode.algorithm.sort;

import com.wy.mycode.algorithm.sort.common.SortExample;
import com.wy.mycode.algorithm.sort.common.TestExampleData;

/**
 * @author HelloWorld
 * @create 2022/6/4 21:17
 * @email helloworld.dng@gmail.com
 * 希尔排序
 */
public class Shell extends SortExample {
    /**
     * @description 希尔排序
     * @author HelloWorld
     * @create 2022/6/4 22:27
     * @param array
     * @return void
     */
    public static void sort(Comparable[] array) {
        int length = array.length;
        int h = 1;

        // h初始化
        while (h < length / 3) {
            // h的可能为:1, 4, 13, 40, 121, 364, 1093...
            h = 3 * h + 1;
        }

        while (h >= 1) {
            // 对间隔为h的子序列进行插入排序
            // 由于希尔排序的 h 一定存在一个值 1, 即对整个array进行插入排序,故,只需要将原插入逻辑改写

            /*  插入排序对逻辑,只需要将 1 换为h即可
                 for (int i = 1; i < array.length; i++) {
                     for (int j = i; j > 0 && less(array[j], array[j-1]); j--) {
                        exch(array, j, j-1);
                     }
                 }
             */
            for (int i = h; i < length; i++) {
                for (int j = i; j >= h && less(array[j], array[j-h]); j = j - h) {
                   exch(array, j, j-h);
                }
            }
            h /= 3;
        }
    }

    public static void main(String[] args) {
        TestExampleData[] array = TestExampleData.getTestData(21);
        show("排序前", array);
        sort(array);
        if (isSorted(array)) {
            show("排序后", array);
            return;
        }
        System.out.println("排序失败");

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值