Implement the search algorithm you came up with in pseudocode with Python Test the search algorithm with a list of 10,100,1000 random numbers (sorted with your sorting algorithm) and compare the result using the %time to time your code and submit your results in code comments
In [ ]:
#search algorithm
#input: a sorted list and a search term
#operation: find the element in that list
#output: location of the item in the list or indicate if it's not there
# the binary search will start by examing in the middle term
In [7]:
def binarysearch(sorted_list, item):
if len(sorted_list)==0:
return False
else:
start=0
end=len(sorted_list)-1
midpoint=(start+end)/2
midpoint = int(midpoint)
print(midpoint)
if sorted_list[midpoint]==item:
return True
else:
if item < sorted_list[midpoint]:
end=midpoint-1
else:
start=midpoint+1
In [8]:
sorted_list=[0, 1, 2, 8, 13, 17, 19, 32, 42]
print(binarysearch(sorted_list,13))
In [14]:
import random
In [15]:
list10 = []
for x in range(10):
list10.append(random.randrange(100))
list10.sort()
In [16]:
list100 = []
for x in range(100):
list100.append(random.randrange(100))
list100.sort()
In [17]:
list1000 = []
for x in range(1000):
list1000.append(random.randrange(100))
list1000.sort()
In [19]:
%time binarysearch(list10, 5)
In [20]:
%time binarysearch(list100, 50)
In [21]:
%time binarysearch(list1000, 500)
In [ ]: