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:

  • We start with a sorted list and a number (a search term).
  • Loop over the list and see if the search term is equal to the item.
  • If it is, return the index. (Using a counter? or len(list) - 1)
  • If the loop has not returned anything, return not found.

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]:
1

In [78]:
#Testing again
look_for_index(2823, [834728934, 2823, 967596, 2323434])

#Yes it works


Out[78]:
0

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


CPU times: user 4 µs, sys: 1e+03 ns, total: 5 µs
Wall time: 16.9 µs

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


CPU times: user 2 µs, sys: 1 µs, total: 3 µs
Wall time: 6.91 µs

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


CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 6.91 µs

In [ ]: