首页 > 编程语言 > 详细

希尔排序

时间:2018-07-05 10:08:09      阅读:177      评论:0      收藏:0      [点我收藏+]
package Sort;

import java.util.Arrays;

public class ShellSort {

    public static void main(String[] args) {
        int[] a = { 54, 35, 48, 36, 27, 12, 44, 44, 8, 14, 26, 17, 28 };
        sort(a);
        System.out.println(Arrays.toString(a));
    }

    public static void sort(int[] a) {
        // 设置步长,默认为数组长度的一半
        int step = a.length / 2;
        while (step >= 1) {
            for (int i = step; i < a.length; i += step) {
                int tmp = a[i];
                int j;
                for (j = i; j > 0 && a[j - step] > tmp; j -= step) {
                    a[j] = a[j - step];//元素后移
                }
                a[j] = tmp;//插入的位置,注意此时j在for循环中已经进行了一次--
            }
            step /= 2;
        }
    }

}

 

希尔排序

原文:https://www.cnblogs.com/yingpu/p/9266597.html

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