Объясните рекурсию в этом алгоритме?

Я знаю, что этот алгоритм предназначен для поиска мажоритарного элемента любого массива, если он есть. Может ли кто-нибудь объяснить вызовы рекурсии?

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? Я не могу понять эти рекурсии. Пожалуйста, объясните на примере, спасибо.


person Community    schedule 04.06.2016    source источник