【问题标题】:Can't solve Time complexity of this exponential sorting algorithm无法解决这种指数排序算法的时间复杂度
【发布时间】:2014-04-11 17:07:30
【问题描述】:
A[n];
Sort(p,r) 
{
    if (A[p] > A[r]) then
        A[p]<->A[r];    //exchange
    if (p+1 >= r) then
        return;
    q <- (r-p+1) / 3;
    Sort(p, r-q);    // 2/3 of head
    Sort(p+q, r);    // 2/3 of tail
    Sort(p, r-q);    // again, 2/3 of head
 }

大家好。 这是我学习的问题。 该算法适用于排序。 n time 15 0.004 16 0.008 17 0.017 18 0.034 19 0.072 20 0.143 21 0.283 22 0.572 23 1.154 24 2.296 25 4.604 26 9.23 27 18.517 以上是n的时间。 (示例:如果 n 为 15,则像这样工作。Sort(0,14))

时间复杂度似乎是 2^n 指数。对吧?

但我不知道它是怎样的,因为我认为它是 T(n) = 3T((2/3)*n) + 1 = 3^n。 它与我所拥有的实时不匹配... 需要帮助,请。

【问题讨论】:

  • 你确定你对重复的限制很严格吗?

标签: algorithm sorting time-complexity exponential


【解决方案1】:

我不知道该说什么...
我查看了您发布的代码,我认为T(n) = 3⋅T((2⋅n/3) + 1 是正确的。 但是然后主定理说T(n) = O(n<sup>log<sub>3/2</sub>(3)</sup>) ≈ O(n<sup>2.7095</sup>) 或类似的东西。我根本看不到指数时间。所以我用PHP写了一个测试。

function WiredSort($p, $r, &$A)
{
    if($A[$p] > $A[$r])
    {
        $temp = $A[$p];
        $A[$p] = $A[$r];
        $A[$r] = $temp;
    }

    if($p + 1 >= $r)
        return;

    $q = round(($r-$p+1)/3);
    WiredSort($p, $r-$q, $A);
    WiredSort($p+$q, $r, $A);
    WiredSort($p, $r-$q, $A);
}

调用WiredSort(0,n-1,$A) 运行
n=100 在 0.056003 秒内,
n=110 在 0.056003 秒内,
n=1000 在 0.390766 秒内。

这在我看来也不是指数级的。

您从哪里获得数据?

【讨论】:

    【解决方案2】:

    根据方程 T(n) = 3*T(2/3*n) + 1 上使用大师定理的时间复杂度,我们得到 T(n) = O(n^(log(3/2)(3)) = O(n^(2.7)) 而不是 O(3^n),因此您在实现问题时存在严重错误,否则您无法获得该算法的指数增长

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多