Travis Mayberry & Sue Evans
Import random and time From search import * 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
1.
import
random
2.
...
3.
number = random.randint(
1
,
1001
)
4.
print
"%7d"
% (number),
cp /afs/umbc.edu/users/s/l/slupoli/pub/labCode201/search.py .
from search import *
Here's an example of timing:
01.
import
time
02.
03.
t0 = time.time()
04.
y =
1
05.
06.
for
x
in
range(
1000
):
07.
y = y * x
08.
09.
t1 = time.time()
10.
11.
print
"Execution time:"
, t1 - t0