Skip to content

Commit

Permalink
Merge pull request #1419 from Aayushs1602/Aayushs1602-patch-1
Browse files Browse the repository at this point in the history
Binary Search in python
  • Loading branch information
fineanmol committed Sep 30, 2022
2 parents e090e43 + 6a019a8 commit 65fd9e7
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#taking input
arr = list(map(str, input("enter array").split()))

#element to search
element = input("enter element to search")

#implementing binary search
left = 0
right = len(arr)-1

check = 0

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

if arr[mid] == element: #if element is found
check = 1
break
elif arr[mid] > element: # if element is less than arr[mid]
right = mid - 1
else: # if element is greater than arr[mid]
left = mid + 1

print("element found") if check else print("element not found") #print found if check == 1 else not found

0 comments on commit 65fd9e7

Please sign in to comment.