代码拉取完成,页面将自动刷新
# It returns location of x in given array arr
# if present, else returns -1
def binarySearch(arr, l, r, x):
while l <= r:
mid = l + (r - l) / 2 #extracting the middle element from the array
mid=int(mid) #it has to be integer
# Check if x is present at mid
if arr[mid] == x:
return mid
# If x is greater, ignore left half
elif arr[mid] < x:
l = mid + 1 #l is initialised to the rightmost element of the middle so that the search could be started from there the next time
# If x is smaller, ignore right half
elif x<arr[mid]:
r = mid - 1 #r is initialised to the leftmost element of the middle so that the search goes till there only the next time
# If we reach here, then the element was not present
return -1
# Main Function
if __name__ == "__main__":
# User input array
print("Enter the array with comma separated in which element will be searched")
arr =[int(x) for x in input().split(',')] #the input array will of int type with each element seperated with a comma due to the split fucntion
#map function returns a list of results after applying the given function to each item
x = eval(input("Enter the element you want to search in given array"))
# Function call
result = binarySearch(arr, 0, len(arr) - 1, x)
#printing the output
if result != -1:
print("Element is present at index {}".format(result))
else:
print("Element is not present in array")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。