- Notifications
You must be signed in to change notification settings - Fork 638
Expand file tree
/
Copy pathP22_SequentialSearch.py
More file actions
Latest commit
23 lines (21 loc) · 678 Bytes
/
Copy pathP22_SequentialSearch.py
File metadata and controls
23 lines (21 loc) · 678 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#Author: OMKAR PATHAK
#This program is an example for sequential search
defsequentialSearch(target, List):
'''This function returns the position of the target if found else returns -1'''
position=0
globaliterations
iterations=0
whileposition<len(List):
iterations+=1
iftarget==List[position]:
returnposition
position+=1
return-1
if__name__=='__main__':
List= [1, 2, 3, 4, 5, 6, 7, 8]
target=3
ans=sequentialSearch(target, List)
ifans!=-1:
print('Target found at position:',ans,'in',iterations,'iterations')
else:
print('Target not found in the list')