Skip to content

Commit dce6026

Browse files
authored
Create linear-search
1 parent 52d62ac commit dce6026

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

linear-search

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def linear_search(arr, target):
2+
for i in range(len(arr)):
3+
if arr[i] == target:
4+
return i # Target found, return its index
5+
return -1 # Target not found
6+
7+
# Example usage:
8+
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
9+
target_value = 9
10+
11+
result = linear_search(my_list, target_value)
12+
13+
if result != -1:
14+
print(f"Element {target_value} is present at index {result}.")
15+
else:
16+
print(f"Element {target_value} is not present in the list.")

0 commit comments

Comments
 (0)