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
22 changes: 22 additions & 0 deletions BinarySearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Binary Search in python

def binarySearch(array, x, low, high):

# Repeat until the pointers low and high meet each other
while low <= high:
mid = low + (high - low)//2
if array[mid] == x:
return mid
elif array[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1

array = [1,2,3,4,5,6,7,8,9]
x = input("Enter your value: ")
result = binarySearch(array, int(x), 0, len(array)-1)
if result != -1:
print(x + "\tElement is present at index " + str(result))
else:
print("Not found")