首页 > 编程语言 > 详细

希尔排序java实现

时间:2019-03-15 18:05:35      阅读:154      评论:0      收藏:0      [点我收藏+]

package notes.javase.algorithm.sort;

 

public class ShellSort {

    public void shellSort(int[] list) {

        int gap = list.length / 2;

 

        while (1 <= gap) {

            // 把距离为 gap 的元素编为一个组,扫描所有组

            for (int i = gap; i < list.length; i++) {

                int j = 0;

                int temp = list[i];

 

                // 对距离为 gap 的元素组进行排序

                for (j = i - gap; j >= 0 && temp < list[j]; j = j - gap) {

                    list[j + gap] = list[j];

                }

                list[j + gap] = temp;

            }

 

            System.out.format("gap = %d:\t", gap);

            printAll(list);

            gap = gap / 2; // 减小增量

        }

    }

 

    // 打印完整序列

    public void printAll(int[] list) {

        for (int value : list) {

            System.out.print(value + "\t");

        }

        System.out.println();

    }

 

    public static void main(String[] args) {

        int[] array = {

                9, 1, 2, 5, 7, 4, 8, 6, 3, 5

        };

 

        // 调用希尔排序方法

        ShellSort shell = new ShellSort();

        System.out.print("排序前:\t\t");

        shell.printAll(array);

        shell.shellSort(array);

        System.out.print("排序后:\t\t");

        shell.printAll(array);

    }

}

希尔排序java实现

原文:https://www.cnblogs.com/mznsndy/p/10538447.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!