Question: 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
My pseudocode:
In [67]:
import random
from bisect import bisect
In [74]:
def look_for_index(search_term, list_of_numbers):
sorted_numbers = sorted(list_of_numbers)
#return sorted_numbers #Check if it does. Yes it does.
index = bisect(sorted_numbers, search_term) - 1
return index
In [75]:
#Testing
look_for_index(2, [1,5,2,4,3])
Out[75]:
In [78]:
#Testing again
look_for_index(2823, [834728934, 2823, 967596, 2323434])
#Yes it works
Out[78]:
In [79]:
list_of_ten_numbers = random.sample(range(1,1000000), 10)
def look_for_index(search_term, list_of_ten_numbers):
sorted_ten_numbers = sorted(list_of_ten_numbers)
index = bisect(sorted_ten_numbers, search_term) - 1
return index
%time
In [80]:
list_of_hundred_numbers = random.sample(range(1,1000000), 100)
def look_for_index(search_term, list_of_hundred_numbers):
sorted_hundred_numbers = sorted(list_of_hundred_numbers)
index = bisect(sorted_hundred_numbers, search_term) - 1
return index
%time
In [81]:
list_of_thou_numbers = random.sample(range(1,1000000), 1000)
def look_for_index(search_term, list_of_thou_numbers):
sorted_thou_numbers = sorted(list_of_thou_numbers)
index = bisect(sorted_thou_numbers, search_term) - 1
return index
%time
In [ ]: