Skip to content
Open
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
23 changes: 23 additions & 0 deletions Problems/Arrays/search-binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#author: seema kumari patel
#implementation of binary search algorithm in python

def binary_search(arr, target):
left, right = 0, len(arr) - 1

while left <= right:
mid = left + (right - left) // 2

# Check if target is present at mid
if arr[mid] == target:
return mid
# If target is greater, ignore left half
elif arr[mid] < target:
left = mid + 1
# If target is smaller, ignore right half
else:
right = mid - 1

# Target was not found in the array
return -1

binary_search([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)