We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ab5f01d commit 20c1c4eCopy full SHA for 20c1c4e
algorithms/search/binary_search.py
@@ -1,9 +1,13 @@
1
#
2
# Binary search works for a sorted array.
3
# Note: The code logic is written for an array sorted in
4
-# increasing order.
5
-# T(n): O(log n)
6
-#
+# 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)
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
11
def binary_search(array, query):
12
lo, hi = 0, len(array) - 1
13
while lo <= hi:
0 commit comments