-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSequentialSearch.py
58 lines (43 loc) · 1.24 KB
/
SequentialSearch.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
52
53
54
55
56
57
58
__author__ = 'Sanjay'
def seqSearch(aList, item):
pos =0
found = False
while pos<len(aList) and not found:
if aList[pos] == item:
found = True
else:
pos = pos +1
return found
#as per book
def sequentialSearch(alist, item):
pos = 0
found = False
while pos < len(alist) and not found:
if alist[pos] == item:
found = True
else:
pos = pos+1
return found
#sequential search for a ordered list.
def orderedSequentialSearch(alist, item):
pos = 0
found = False
stop = False
while pos < len(alist) and not found and not stop:
if alist[pos] == item:
found = True
else:
if alist[pos] > item:
stop = True
else:
pos = pos+1
return found
testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,]
print(orderedSequentialSearch(testlist, 3))
print(orderedSequentialSearch(testlist, 13))
if __name__ == '__main__':
s= [1,2,32,42,12,4,3,45,454]
print(seqSearch(s,45))
testlist = [1, 2, 32, 8, 17, 19, 42, 13, 0]
print(sequentialSearch(testlist, 3))
print(sequentialSearch(testlist, 13))