Saturday, 10 November 2012

CSC236 Fall 2012 Quiz #5 (My solution)

BinSearch(A, first, last)
         if A[first] == A[last]                                   ---------------------1
                  return A[first]                                   ---------------------2
         mid = floor((first+last)/2)                        ---------------------3

         if A[mid] > A[mid + 1]                               ---------------------4
                   return BinSearch(A,first,mid)      ---------------------5
       else
                   return BinSearch[A,mid,last]       ---------------------6

Lines 1-4 will execute once, so it will be of theta(1) at max.
The rest of the code approximately runs around half times the length of n,
so we will write that as the ceiling of n/2, as we dont' want to lost any iterations
by using the floor value.

This creates


T {theta(1)                      n = 1
    theta(ceil(n/2))          n>1
    }

Then T(n) = theta(1) + Theta(ceil(n/2))
Then a = 1
Then b = 2
Then d = 0




      

1 comment:

  1. I don't think that's quite right... it looks like T(n) should be Theta(log n)?

    ReplyDelete