【发布时间】:2016-10-04 15:07:26
【问题描述】:
我知道此算法用于查找任何数组的多数元素(如果有的话)。谁能解释一下递归调用?
if length(A) = 0 then
return null
end if
if length(A) = 1 then
return 1
end if
// "Command 7"
Call FIND-MAJORITY recursively on the first half of A, and let i be the result.
// "Command 8"
Call FIND-MAJORITY recursively on the second half of A, and let j be the result.
if i > 0 then
compare i to all objects in A(including itself);
let k be the number of times that equality holds;
if k > length(A)/2 then
return i.
end if
end if
if j > 0 then
compare j to all objects in A(including itself);
let k be the number of times that equality holds;
if k > length(A)/2 then
return j
end if
end if
return null
是否执行命令 7 直到它得到一个值……然后是命令 8?我无法理解这些递归。请举例说明,谢谢。
【问题讨论】:
标签: recursion divide-and-conquer