147k views
2 votes
Using the programming language of your choice, implement the Binary Search algorithm for a target value = 9 on the Array A: [9, 11, 70, 25, 20, 0, 36, 24]. What is the primary condition to implement a Binary Search Algorithm? Explain the growth rate of the algorithm

User Modesitt
by
4.1k points

1 Answer

5 votes

Answer:

myArray = [9, 11, 70, 25, 20, 0, 36, 24]

myvalue = 20

def binary_search(mylist, value):

sorted(mylist)

mid = mylist[round(len(mylist) / 2)]

if value == mid:

return mylist.index(mid)

elif value < mid:

for index, s_one in enumerate(mylist[ : (mylist.index(mid))]):

if s_one == value:

return index

elif value < mid:

for index, s_two in enumerate(mylist[(mylist.index(mid)) : ]):

if s_two == value:

return index

else:

return "searched value not in list/array"

result = binary_search( myArray, myvalue)

print(f"Index of the searched value {myvalue} is: {result}")

Step-by-step explanation:

The programming language used above is python. It is used to implement a binary search in a list and finally returns the index of the searched value.

User Jackotonye
by
4.4k points