0
Function(A, n)
/* A is an array of integers
/* random is a function that returns an integer between 1 and (in this case) n-1
if(n<=1) then return (A[1])
else
x←0;
for i←1 to n-1 do
A[i]←A[i]-A[i+1]
x←x+A[i]
end
k←Random(n-1)
x←x+Function(A,k)
x←x+Function(A,n-k)
return(x)
end
我不明白爲什麼這種算法的最壞情況是當k = 1或n-1時,最好的情況是當k = n/2時。如何確保2ET(n/2)的預期運行時間小於ET(n-1)?包含兩次遞歸調用算法的預期運行時間
提示:做一些代數摸出(說)了多少次'X←X + A [1]'爲執行在您確定的3個案例中給定N個。 (通過使N爲2的冪來簡化代數) –
[this]的可能重複(https://stackoverflow.com/questions/44223411/expected-running-time-of-the-algorithm-containing-two遞歸調用)遞歸問題 –
這是一個老玩笑。這很有趣*第一次*時間.... –