【问题标题】:Time complexity for Shell sort using Knuth's sequence?使用 Knuth 序列进行 Shell 排序的时间复杂度?
【发布时间】:2013-01-04 22:34:10
【问题描述】:

我的 shell 排序实现的时间复杂度是多少?

void shellsort(int items[],int n)   
{
    int j=1;
    int d = (pow(3.0,j)-1) / 2;

    while(d < ceil(n/3))
    {
        for(int i=d;i<n;i++)
        {
            item tmp = items[i];
            int k;
            for(k = i; k >= d && items[k-d] > tmp; k -= d)
                items[k]=items[k-d];
            items[k]=tmp;
        }

        j++;
        d = (pow(3.0,j)-1) / 2;
    }

}

【问题讨论】:

    标签: sorting time-complexity shellsort


    【解决方案1】:

    Knuth 根据公式 (3k – 1) / 2 或 [1, 4, 14, 40, 121, ...] 提出了自己的增量序列。

    使用 Knuth 递增序列进行 shell 排序的时间复杂度为 O(n^3/2)。

    来源:https://web.archive.org/web/20181026010135/http://www.stoimen.com:80/blog/2012/02/27/computer-algorithms-shell-sort/

    【讨论】:

    • 链接被破坏,这就是不推荐使用它们的原因。下次尝试提供更多信息,即关于如何达到 O(n^3/2) 的小总结
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 2013-10-07
    相关资源
    最近更新 更多