Skip to content

Commit 20c1c4e

Browse files
authored
Update binary_search.py (#657)
1 parent ab5f01d commit 20c1c4e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

algorithms/search/binary_search.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#
22
# Binary search works for a sorted array.
33
# Note: The code logic is written for an array sorted in
4-
# increasing order.
5-
# T(n): O(log n)
6-
#
4+
# increasing order.
5+
#For Binary Search, T(N) = T(N/2) + O(1) // the recurrence relation
6+
#Apply Masters Theorem for computing Run time complexity of recurrence relations : T(N) = aT(N/b) + f(N)
7+
#Here, a = 1, b = 2 => log (a base b) = 1
8+
# 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))
9+
10+
711
def binary_search(array, query):
812
lo, hi = 0, len(array) - 1
913
while lo <= hi:

0 commit comments

Comments
 (0)