diff --git a/algorithms/search/binary_search.py b/algorithms/search/binary_search.py index 644f98f8e..d8bccf06b 100644 --- a/algorithms/search/binary_search.py +++ b/algorithms/search/binary_search.py @@ -1,9 +1,13 @@ # # Binary search works for a sorted array. # Note: The code logic is written for an array sorted in -# increasing order. -# T(n): O(log n) -# +# increasing order. +#For Binary Search, T(N) = T(N/2) + O(1) // the recurrence relation +#Apply Masters Theorem for computing Run time complexity of recurrence relations : T(N) = aT(N/b) + f(N) +#Here, a = 1, b = 2 => log (a base b) = 1 +# also, here f(N) = n^c log^k(n) //k = 0 & c = log (a base b) So, T(N) = O(N^c log^(k+1)N) = O(log(N)) + + def binary_search(array, query): lo, hi = 0, len(array) - 1 while lo <= hi: