【问题标题】:Recursive Merge Sort Algorithm递归合并排序算法
【发布时间】:2015-02-25 06:50:10
【问题描述】:

所以我正在研究一个算法问题,并且对正确答案应该是什么样子感到非常困惑。我有一个答案,但如果有人能给我反馈/指导,我将不胜感激。

问题:

Casc Merge is a recursive algorithm: Assume there     
are n sorted lists, each of size m. Recursively 
Casc Merge the first n − 1 lists and then
Merge the last list (of size m) into the sorted list A (which is of size (n-1)m).

1)Write down code for it

这是我目前所想到的。看来我希望自己走在正确的轨道上,但就像我说的那样,我不知道。我尝试谷歌搜索并没有得到太多帮助

proc Cas(A, L, c)
  if c == n then
     Merge(A, L[c-1], L[c])
  end if
  else
     Merge(A, Casc(A, L, c), Casc=(A, L, c+1))
  end else
end proc 

再次感谢您对伪代码的任何建议/反馈。

假设合并进行 m + n - 1 次比较

S(n) = { 1                  if c = 1   
         S(n-1) + m - 1     otherwise   
}

【问题讨论】:

    标签: algorithm sorting merge mergesort


    【解决方案1】:

    我认为你很接近。虽然,看起来您可能在您的基本案例附近有一个“双重预订”合并,其中c==n,因为您将L[c-1]L[c] 合并到A,但在之前的电话中,您已经在执行 L[c-1]L[c] 的合并。

    如果您要根据定义来考虑递归算法,您可以使用稍微简化的逻辑将其写出来:

    procedure Cascade(List A, List[] L, int c)
       // base case (1-based indexing)
       if (c == 1) then
           Merge(A, L[c], {}) // merge the empty list with L[c] into A
       else
           // use the recursive definition
           Merge(A, L[c], Cascade(A, L, c-1))  // merge L[c] with L[c-1]
       end if
    end procedure
    

    你会调用这样的过程:Cascade({}, L, n)

    你可以这样处理它:

    n = 3
    L = {{1 2 3} {3 2 1} {4 5 6}}
    A = {}
    
    First call to Merge yields:
    Merge(A, {4 5 6}, Cascade({}, {{1 2 3} {3 2 1} {4 5 6}}, 2))
    
    Then:
    n = 2
    Merge(A, {3 2 1}, Cascade({}, {{1 2 3} {3 2 1} {4 5 6}}, 1))
    
    Then:
    n = 1 (base case)
    Merge(A, {1 2 3}, {})
    
    Trickling back up the chain (actual merge results not shown for clarity):
    A = {1 2 3}
    A = {1 2 3 3 2 1}
    A = {1 2 3 3 2 1 4 5 6}  // merged list (the actual implementation would have sorted these...)
    

    你就完成了!希望对您有所帮助...

    编辑:根据 cmets 中的详细讨论,以下是一个使用返回来传递数据的示例,而不是就地修改。

    procedure Cascade(List[] L, int c)
       // base case (1-based indexing)
       if (c == 1) then
           A = Merge(L[c], {}) // merge the empty list with L[c] into A
       else
           // use the recursive definition
           A = Merge(L[c], Cascade(L, c-1))  // merge L[c] with L[c-1]
       end if
       Return A // return the newly merged list
    end procedure
    

    【讨论】:

    • 啊,好的,我明白了,谢谢。无论如何,您可以帮我推导出算法使用的确切比较次数的递归吗?我不知道该怎么做,因为比较是在合并中完成的?任何帮助都会很棒
    • 基本思路是判断Merge会被调用多少次,然后计算每次调用的比较次数。如果您仍然无法找到答案,我可以尝试进一步帮助您。
    • 好的,我很确定我知道该怎么做。但我需要先改变算法。我刚刚发现我应该使用返回 a 和 b 的合并列表的 merge(a, b) 方法。所以我不需要3个参数。那么这会将其更改为 Merge(A, L[c]) 和 Merge(L[c], Cascade(A, L, c-1)) 吗?在解决不正确的算法之前,我想仔细检查。再次感谢您迄今为止的所有帮助
    • 我建议也将 A 作为参数从 Cascade 中删除,它还​​会返回合并的列表,但这取决于您。假设所有项目都是不可变的,并使用返回将数据传回给调用者,这可能是一种更好的做法。 Merge(A, L[], L[]) 然后变成A = Merge(L[],L[])
    • 我没有百分百关注你。我用我当前的答案编辑了你的答案。无论如何,您可以显示您正在解释的内容吗?如果在参数中删除了 A,我将如何设置它
    猜你喜欢
    • 2015-07-12
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 2019-01-22
    相关资源
    最近更新 更多