Skip to content

Instantly share code, notes, and snippets.

@IanMcT
Created November 14, 2016 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanMcT/45384edceafd219a3a5944c9e53b09ec to your computer and use it in GitHub Desktop.
Save IanMcT/45384edceafd219a3a5944c9e53b09ec to your computer and use it in GitHub Desktop.
Demo of linear search
#I McTavish
#Nov 13, 2016
#Linear search - measure how long a linear search takes
import datetime #used to track time
def linear_search(value, list):
for i in range(len(list)):
if list[i] == value:
return i
return "item not found"
number_of_items = int(input("How many items? "))
master_list = []
for x in range(number_of_items):
master_list.append(x)
print("Best case Start time: ")
print(datetime.datetime.now())
linear_search(0,master_list)
print("Best case End time: ")
print(datetime.datetime.now())
print("Worst case Start time: ")
print(datetime.datetime.now())
linear_search(len(master_list),master_list)
print("Worst case End time: ")
print(datetime.datetime.now())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment