我必须使用名为Barometer Operation的方法找到Merge Sort的指令计数。
什么是晴雨表操作?
例如: -
for(int i=0;i < n;i++)
A[i] = i + 1
循环体气压计操作= +
count(+)= n
所以现在Merge Sort的问题在于它是递归算法,我不知道如何选择一条特定指令,这样我就可以计算执行特定指令的次数。
答案 0 :(得分:0)
感谢 @aphex 和 @ SGM1 向我展示正确的方向。我想解决问题的方法如下:
我使用了与 aphex 相同的代码。但只有解决方案的问题是 aphex 忘记在最坏的情况下保持它。所以根据我的气压计操作/说明必须
first(left) <= first(right)
因为这是进行实际比较的地方。晴雨表操作员将是“&lt; =”,其中进行实际比较。并且这个运算符通过两个方法递归调用(n / 2)次,这两个方法将列表分开然后主合并操作恰好发生(n-1)次。因此在求解求和方程后它给出(nlog(n)+ c)
来自Wiki的代码,很少通过 aphex
进行修改function merge_sort(list m, var count)
if(length(m))>0
count++;
// if list size is 0 (empty) or 1, consider it sorted and return it
// (using less than or equal prevents infinite recursion for a zero length m)
if length(m) <= 1
return m
// else list size is > 1, so split the list into two sublists
var list left, right
var integer middle = length(m) / 2
for each x in m before middle
add x to left
for each x in m after or equal middle
add x to right
// recursively call merge_sort() to further split each sublist
// until sublist size is 1
left = merge_sort(left,count)
right = merge_sort(right,count)
// merge the sublists returned from prior calls to merge_sort()
// and return the resulting merged sublist
return merge(left, right)
function merge(left, right)
var list result
while length(left) > 0 or length(right) > 0
if length(left) > 0 and length(right) > 0
if first(left) <= first(right) // My Barometer Operation
append first(left) to result
left = rest(left)
else
append first(right) to result
right = rest(right)
else if length(left) > 0
append first(left) to result
left = rest(left)
else if length(right) > 0
append first(right) to result
right = rest(right)
end while
return result
答案 1 :(得分:-1)
让我们使用Wikipedia中的代码。你需要指望分裂部分。分割是在merge_sort(list m)
中进行的。您需要将计数器添加为输入参数(var count
),如下所示。该计数器必须为count=0
。每次调用方法和列表lenght>0
时,都会将计数器增加1.
function merge_sort(list m, var count)
// Count here
if(length(m))>0
count++;
// if list size is 0 (empty) or 1, consider it sorted and return it
// (using less than or equal prevents infinite recursion for a zero length m)
if length(m) <= 1
return m
// else list size is > 1, so split the list into two sublists
var list left, right
var integer middle = length(m) / 2
for each x in m before middle
add x to left
for each x in m after or equal middle
add x to right
// recursively call merge_sort() to further split each sublist
// until sublist size is 1
left = merge_sort(left,count)
right = merge_sort(right,count)
// merge the sublists returned from prior calls to merge_sort()
// and return the resulting merged sublist
return merge(left, right)
function merge(left, right)
var list result
while length(left) > 0 or length(right) > 0
if length(left) > 0 and length(right) > 0
if first(left) <= first(right)
append first(left) to result
left = rest(left)
else
append first(right) to result
right = rest(right)
else if length(left) > 0
append first(left) to result
left = rest(left)
else if length(right) > 0
append first(right) to result
right = rest(right)
end while
return result
对于维基百科示例,您需要调用伪代码,如merge_sort([38,27,43,3,9,82,10], 0)
。