-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearSearch.py
More file actions
35 lines (29 loc) · 1000 Bytes
/
linearSearch.py
File metadata and controls
35 lines (29 loc) · 1000 Bytes
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
class LinearSearch:
"""
Linear Search Algorithm:
Time Complexity: O(n)
Space Complexity: O(1)
"""
def __init__(self, array, target):
self.array = array
self.target = target
def search(self):
if(len(self.array) == 0):
raise Exception("Array is empty")
for i in range(len(self.array)):
if self.array[i] == self.target:
return i
return -1
def __str__(self):
try:
index = self.search()
except Exception as e:
return f"LinearSearch: {e}"
if index != -1:
return f"LinearSearch: For array ={self.array} with target Value = {self.target} Found at index {index}"
return f"LinearSearch: For array ={self.array} with target Value = {self.target} Not Found"
if __name__ == "__main__":
array = [1, 2, 3, 4, 5, 6]
target = 5
linear_search = LinearSearch(array, target)
print(linear_search)