-
Notifications
You must be signed in to change notification settings - Fork 91
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
1 parent
693cdf3
commit fc9eba3
Showing
1 changed file
with
23 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,23 @@ | ||
arr = list(map(int, input("Enter elements:").split())) | ||
elem = int(input("Enter element to search:")) | ||
|
||
def binary_search(L, x): | ||
|
||
l = 0 | ||
r = len(L) - 1 | ||
m = 0 | ||
while l <= r: | ||
m = (r + l) // 2 | ||
if L[m] < x: | ||
l = m + 1 | ||
elif L[m] > x: | ||
r = m - 1 | ||
else: | ||
return m | ||
return -1 | ||
|
||
res = binary_search(arr, elem) | ||
if res == -1: | ||
print("Element not found") | ||
else: | ||
print("Element found at index:",res) |