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
43 changes: 43 additions & 0 deletions Binary/binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Recursive Binary Search algorithm in Python

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

if high >= low:

mid = low + (high - low)//2

# If found at mid, return the value

if array[mid] == x:

return mid

# Search the first half

elif array[mid] > x:

return binarySearch(array, x, low, mid-1)

# Search the second half

else:

return binarySearch(array, x, mid + 1, high)

else:

return -1

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

x = int(input("Enter a number between 1 and 10:"))

result = binarySearch(array, x, 0, len(array)-1)

if result != -1:

print("Element is present at position" + str(result))

else:

print("Element not found")