Skip to content

Commit

Permalink
Create Binary_Search.py (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
Karna0005 authored Oct 6, 2024
1 parent 9fd4d4a commit bd58c1c
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Python/Binary_Search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def binary_search(arr, target):
left, right = 0, len(arr) - 1

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

if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1

return -1

arr = list(map(int, input("Enter a sorted list of numbers separated by spaces: ").split()))
target = int(input("Enter the target number: "))

result = binary_search(arr, target)
print(f"Element found at index: {result}" if result != -1 else "Element not found")

0 comments on commit bd58c1c

Please sign in to comment.