Computer Science, asked by krish22223, 10 months ago

tuple question ! WAP to search a given target value in ascending order in the tuple using binary search [PYTHON]

Answers

Answered by manavjaison
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 Anonymous
0

Tuples are immutable i.e the value cannot be changed. So, Option D is correct.

Similar questions