Skip to content

Commit 4269bdf

Browse files
committed
Add simple linear search algorithm
1 parent 2c15b8c commit 4269bdf

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

searches/linear_search_simple.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""
2+
Linear Search Algorithm
3+
4+
Linear search (also known as sequential search) is a simple searching algorithm
5+
that checks each element in a collection sequentially until it finds the target
6+
value or reaches the end of the collection.
7+
8+
How it works:
9+
1. Start from the first element in the collection
10+
2. Compare the current element with the target value
11+
3. If they match, return the index of the current element
12+
4. If they don't match, move to the next element
13+
5. Repeat steps 2-4 until the target is found or all elements have been checked
14+
6. If the target is not found after checking all elements, return -1
15+
16+
Time Complexity:
17+
- Best case: O(1) - when the target is found at the first position
18+
- Average case: O(n) - where n is the number of elements
19+
- Worst case: O(n) - when the target is at the end or not found
20+
21+
Space Complexity:
22+
- O(1) - only a constant amount of extra space is used (for variables)
23+
"""
24+
25+
26+
from typing import Any, List, Optional
27+
28+
29+
def linear_search(collection: List[Any], target: Any) -> int:
30+
"""
31+
Perform a linear search to find the target value in the collection.
32+
33+
Args:
34+
collection: A list of elements to search through (does not need to be sorted)
35+
target: The value to search for in the collection
36+
37+
Returns:
38+
The index of the target value if found, -1 otherwise
39+
40+
Examples:
41+
>>> linear_search([5, 2, 8, 1, 9], 8)
42+
2
43+
>>> linear_search([5, 2, 8, 1, 9], 3)
44+
-1
45+
>>> linear_search(['apple', 'banana', 'cherry'], 'banana')
46+
1
47+
"""
48+
for index, item in enumerate(collection):
49+
if item == target:
50+
return index
51+
return -1
52+
53+
54+
if __name__ == "__main__":
55+
# Example 1: Searching for a number in a list of integers
56+
numbers = [64, 34, 25, 12, 22, 11, 90]
57+
target_number = 22
58+
result = linear_search(numbers, target_number)
59+
60+
print("Example 1: Searching for a number")
61+
print(f"Collection: {numbers}")
62+
print(f"Target: {target_number}")
63+
if result != -1:
64+
print(f"Result: Found at index {result}")
65+
else:
66+
print("Result: Not found")
67+
print()
68+
69+
# Example 2: Searching for a string
70+
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
71+
target_fruit = "cherry"
72+
result = linear_search(fruits, target_fruit)
73+
74+
print("Example 2: Searching for a string")
75+
print(f"Collection: {fruits}")
76+
print(f"Target: {target_fruit}")
77+
if result != -1:
78+
print(f"Result: Found at index {result}")
79+
else:
80+
print("Result: Not found")
81+
print()
82+
83+
# Example 3: Target not found
84+
numbers = [1, 3, 5, 7, 9]
85+
target_number = 4
86+
result = linear_search(numbers, target_number)
87+
88+
print("Example 3: Target not found")
89+
print(f"Collection: {numbers}")
90+
print(f"Target: {target_number}")
91+
if result != -1:
92+
print(f"Result: Found at index {result}")
93+
else:
94+
print("Result: Not found")
95+
print()
96+
97+
# Example 4: Empty collection
98+
empty_list: List[int] = []
99+
result = linear_search(empty_list, 5)
100+
101+
print("Example 4: Empty collection")
102+
print(f"Collection: {empty_list}")
103+
print(f"Target: 5")
104+
if result != -1:
105+
print(f"Result: Found at index {result}")
106+
else:
107+
print("Result: Not found")

0 commit comments

Comments
 (0)