Travis Mayberry & Sue Evans
import random and time Get length of list from the user Create a list containing the values 0 to length - 1 Shuffle the list with random.shuffle() Start linear search timer Loop 1000 times: Generate a random integer between 0 and length - 1 Call linear search on the list with this random number Stop linear search timer Start binary search timer Sort list with list.sort() Loop 1000 times: Generate a random integer between 0 and length - 1 Call binary search on list with random number Stop binary search timer Print out resulting times
Here's an example of timing:
import time t0 = time.time() y = 1 for x in range(1000): y = y * x t1 = time.time() print "Execution time:", t1 - t0