Search code examples
algorithmrecursioncomplexity-theorydivide-and-conquer

Number execution of recursive function


I have this recursive algorithm and T(n) is the number of times that P(a, b) is executed and n := b - a

int foo[] = ... // array of big enough size
function P(int a, int b) 
    if (a+1 < b) {
       int h = floor((a+b)/2)
       if foo[h] >= 0 then P(a, h)
       if foo[h] <= 0 then P(h, b)
    }
end function

how can I compute T(1), T(2), T(3) and T(4)


Solution

  • As each time the distance between a and b (as an input of the function) is cut half, the time complexity is Theta(log(b-a)).