1
Je sais que cet algorithme est pour trouver l'élément majoritaire de n'importe quel tableau s'il en a. Quelqu'un peut-il s'il vous plaît expliquer les appels de récursivité?Expliquer la récursivité dans cet algorithme?
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
est-commande 7 est exécutée jusqu'à ce qu'il obtenir une valeur unique ... et commande alors 8? Je ne peux pas comprendre ces récursions. S'il vous plaît expliquer avec exemple, merci.