Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions algorithms/search/binary_search.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down