tuple question ! WAP to search a given target value in ascending order in the tuple using binary search [PYTHON]
Answers
Answered by
0
Heya friend,
Here we go !
# to search a given target value in a tuple sorted in ascending order
a = eval (input('Enter a list:'))
a.sort()
print('List after sorting in ascending order :')
print(a)
l=len(a)
target=float(input('Enter any value:'))
top=l-1
bottom=0
while bottom<=top:
mid=(bottom+top)//2
if a[mid]==target:
print('Value found at:'mid+1)
break
elif a[mid]>target:
top=mid-1
else:
bottom = mid +1
else:
print('Value not found')
Note:-
- Keep in mind the indentation of the program
- Binary search is used when we compare the value to be searched with the middle first and then accordingly to the upper or lower part of the list/tuple.
Thanks !
#BAL #answerwithquality
Answered by
0
Tuples are immutable i.e the value cannot be changed. So, Option D is correct.
Similar questions