-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearSearch.py
51 lines (31 loc) · 1.03 KB
/
LinearSearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import time
import timeit
def LinearSearch(A, key):
current = 1
found = False
AlistSize = len(A)
while current <= AlistSize and found != True:
if(A[current] == key):
found = True
print('Found')
else:
current = current + 1
print('Not Found')
return found
A = list(range(0, 100001))
key = 99999
""" t = timeit.default_timer()
# replace this line with appropriate function name
a = LinearSearch(A, key)
elapsed_time = timeit.default_timer() - t
print(elapsed_time) """
t = time.process_time()
# replace this line with appropriate function name
a = LinearSearch(A, key)
elapsed_time = time.process_time() - t
print(elapsed_time)
# ATTENTION READ TIME CASE RESULTS BELOW
# Best case is O(1), runs once and finds match
# Best Case Time Result: 0.00026980, 0.000542499, .00036900
# Worse case is O(n), runs all the way and doesnt find any matches
# Worse case Time Results: 2.546875, 2.640625, 2.453125