-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |