You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Write a Python program to implement a binary search algorithm without using recursion. Use an iterative approach to search for an element in a sorted list.
defbinary_search(arr, target):
left, right=0, len(arr) -1
whileleft<=right:
mid= (left+right) //2
ifarr[mid] ==target:
returnmid# Element found, return its index
elifarr[mid] <target:
left=mid+1# Adjust the left bound
else:
right=mid-1# Adjust the right bound
return-1# Element not found
# Example usage
sorted_list= [1, 3, 5, 7, 9, 11, 13]
target_element=7
result=binary_search(sorted_list, target_element)
ifresult!=-1:
print(f"Element {target_element} found at index {result}.")
else:
print(f"Element {target_element} not found in the list.")