Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def binary_search(arr, elem):
- start, end = 0, len(arr)-1
- while start <= end:
- mid = (start+end)//2
- if elem == arr[mid]:
- return mid
- elif elem < arr[mid]:
- end = mid-1
- else:
- start = mid+1
- else:
- return False
- arr, choice = [], "y"
- print("The List is now Empty...\nPlease Enter some elements into it...\n")
- while choice.lower() == "y":
- elem = int(input("Enter the INTEGER element to be inserted : "))
- arr.append(elem)
- print("\nNow the List contains : ", arr)
- choice = input("\nDo You Want to insert more elements into it? (Y/[n]) : ")
- find = int(input("Enter the Element to be found : "))
- pos = binary_search(arr, find)
- if bool(pos):
- print(f"The element is found at {pos+1} position")
- else:
- raise ValueError("Oops!!! The Given Element is NOT PRESENT in the given list")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement