Search code examples
algorithmsortingpseudocode

Is the worst case scenario for the following sorting algorithm O(n^2)? I used the master theorem


I am supposed to find the worst case time complexity for the following sorting algorithm. Using master theorem, I got O(n^2). I wanted to check if my answer was right.

SomeSort (A, b, e)
   if e = b + 1 then
      if A[b] > A[e] then
         exchange A[b] and A[e]
      end if
   else if e > b + 1 then
       p ←− [(e-b+1)/3]   * the [] represents floor division
       SomeSort (A, b, e − p)
       SomeSort (A, b + p, e)
       SomeSort (A, b, e − p)
end if

Solution

  • The running time recurrence is

    T(n) = 3T(2n/3) = 3T(n/(3/2)),
    

    hence Case 1 of the Master Theorem applies, and the running time is

    Theta(n^(log(3)/log(3/2))) = Omega(n^2.7).