In [1]:
import array as arr
def LinearSearch(A, s, key): #Function for linear search
for i in range(0, s):
if A[i] == key:
return i + 1
return -1
s = int (input('Enter the size of the array: '))
print ('Enter the array elements: ')
A = arr.array('i', [])
for i in range(0, s):
element = int (input())
A.append(element)
key = int(input('Enter the search value: '))
res = LinearSearch(A, s, key)
if res == -1:
print (key, ' not found in the array.')
else:
print (key, ' found at position ', res)
Enter the size of the array: 5 Enter the array elements: 5 10 15 20 25 Enter the search value: 20 20 found at position 4
No comments:
Post a Comment