Created
November 14, 2016 03:45
-
-
Save IanMcT/45384edceafd219a3a5944c9e53b09ec to your computer and use it in GitHub Desktop.
Demo of linear search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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