-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathlinear-search.py
More file actions
23 lines (18 loc) · 857 Bytes
/
linear-search.py
File metadata and controls
23 lines (18 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
Problem Statement:
Given a list of n elements with values and target element, find the index/position of the target in list.
The linear search is a basic search algorithm which searches all the elements in the list and finds the required value.
This is also known as sequential search.
Time Complexity:
Best Case: O(1)
Average Case: O(n)
Worst Case: O(n)
"""
def linearSearch(arr,target):
for i in range(len(arr)): # traversing the list
if arr[i] == target: # comparing the list element with the target value
return i # return the index if list element is equal to the target element
return -1 # if target is not present in the list it will return -1
a = [1,2,4,5,8]
print(linearSearch(a,5)) # it will return 3 as 5 is present at 3rd index
print(linearSearch(a,9)) # it will return -1 since 9 is not present in the list