栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

希尔排序java实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

希尔排序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;


public class Insertion extends SortExample {
    
    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;


public class Shell extends SortExample {
    
    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进行插入排序,故,只需要将原插入逻辑改写

            
            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("排序失败");

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/950601.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号