gpt4 book ai didi

java - 如何使用经验派生的增量进行 shell 排序?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:18:36 25 4
gpt4 key购买 nike

根据 Wikipedia文章中,我正在尝试使用经验派生的增量来执行 Shell 排序以执行 h 排序:

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

关于java - 如何使用经验派生的增量进行 shell 排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37102234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com