import random import time deforderedSequentialSearch(alist, item): pos = 0 found
ID: 3612149 • Letter: I
Question
import random
import time
deforderedSequentialSearch(alist, item):
pos = 0
found = False
stop = False
while pos < len(alist) and not found and notstop:
if alist[pos] ==item:
found = True
else:
if alist[pos] > item:
stop = True
else:
pos = pos+1
returnfound
def main():
start = time.clock()
for count in range(1,100):
printrandom.randint
count =orderedSequentialSearch(alist)
random.randint =orderedSequentialSearch(item)
elapsed = (time.clock()- start)
if found == False:
print item, ("was not found in the list . Time : "),elapsed
else:
print item, ("was found in the the list. Time : "), elapsed
start = time.clock()
for count in range(1,1000):
printrandom.randint
count =orderedSequentialSearch(alist)
random.randint =orderedSequentialSearch(item)
elapsed = (time.clock()- start)
if found == False:
print item, ("was not found in the list . Time : "),elapsed
else:
print item, ("was found in the the list. Time : "), elapsed
start = time.clock()
for count in range(1,10000):
print random.randint
elapsed = (time.clock()- start)
count =orderedSequentialSearch(alist)
random.randint =orderedSequentialSearch(item)
if found == False:
print item, ("was not found in the list . Time : "),elapsed
else:
print item, ("was found in the the list. Time :"),elapsed
main()
Explanation / Answer
import randomfrom Numeric import *import timedef orderedSequentialSearch(alist, item):pos = 0found = Falsestop = Falsewhile pos item:stop = Trueelse:pos = pos+1return founddef main():start = time.clock()for count in range(1,100):item=random.randint(1,10)print random.randintalist = array(random.randint)found = orderedSequentialSearch(alist,item)elapsed = (time.clock() - start)if found == False:print item, ("was not found in the list . Time : "),elapsedelse:print item, ("was found in the the list. Time : "), elapsedstart = time.clock()for count in range(1,1000):item=random.randint(1,10)print random.randintcount = orderedSequentialSearch(alist,item)elapsed = (time.clock() - start)if found == False:print item, ("was not found in the list . Time : "),elapsedelse:print item, ("was found in the the list. Time : "), elapsedstart = time.clock()for count in range(1,10000):item=random.randint(1,10)print random.randintelapsed = (time.clock() - start)count = orderedSequentialSearch(alist,item)if found == False:print item, ("was not found in the list . Time : "),elapsedelse:print item, ("was found in the the list. Time : "),elapsedmain()Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.