Last answered:

13 Dec 2021

Posted on:

13 Dec 2021

0

Does binary search require the list to be ordered?

Does binary search require the list to be ordered? I thought the binary search required the list to be ordered? I believe this to be true if the binary search uses a tree structure with each node splitting the values based on the size of the value.

I noticed the test variable is not ordered. Also the jupyter notebook tested the linear search again and not the binary search algo

1 answers ( 0 marked as helpful)
Posted on:

13 Dec 2021

0

Yes, it needs to be sorted. I added the line my_list.sort() as the first line under the while statement.while first <= last and found == False:

my_list.sort()  # Was not in
the script in the example
midpoint = (first + last)//2

Also, call the binary_search definition (not linear_search) when running it:

print(binary_search(87, test))
print(binary_search(88, test))

will return True and False.

Submit an answer