Can someone revise this code, it just stops after the number is chosen. It is supposed to determine if the number is in thee array, but the program just ends. import random def binary_search(arr, x):...



Can someone revise this code, it just stops after the number is chosen. It is supposed to determine if the number is in thee array, but the program just ends.


import random


def binary_search(arr, x):

low = 0
high = len(arr) - 1
mid = 0


while low <=>
mid = (high + low) // 2
if arr[mid] <>
low = mid + 1
elif arr[mid] > x:
high = mid - 1
else:
return mid
return -1


arr = []
n = int(input("Enter how many elements you want in array?"))
for i in range(1,n):
arr.append(random.randint(1,100))




x = int(input("Enter which value do you seek to find from the array"))


result = binary_search(arr, x)
print("Searching for", x)




if result != -1:

print("Element is present at index", str(result))



Jun 06, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here