如何将经验推导的增量用于shell排序



根据维基百科的文章,我试图用经验推导的增量实现Shell排序,以执行h-sort:

1,4,10,23,57,132,301,701

目前,我正在使用h = 3*h + 1执行排序,以下是我的实现:

    public class Solution
{
    private static final int arr[] = {9,4,5,1,2,8,7,6,12,45,21,34,1,2,3};
    public static void main(String[] args)
    {
        int N = arr.length;
        int h = 1;
        while(h<N/3)
            h = 3*h + 1;
        while(h>=1)
        {
            for(int i=h;i<N;i++)
            {
                for(int j=i;j>=h && arr[j-h]>arr[j];j-=h)
                {
                    int temp = arr[j-h];
                    arr[j-h] = arr[j];
                    arr[j] = temp;
                }
            }
            h/=3;
        }
        for(int x:arr)
            System.out.println(x);
    }
}

现在,这很好地完成了任务。但问题是,如果我要通过使用经验推导的序列来执行h排序来实现shell排序,我应该如何根据数组的大小选择必须使用的增量?

将经验推导的序列存储在数组中,并找到该数组中小于数据数组大小的最后一个元素。

例如,如果数据大小为500,则必须获得301作为第一步

最新更新