Skip to content

Commit 25c149d

Browse files
author
Muh. Angga Muttaqien
committed
add linear search
1 parent 918096d commit 25c149d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

searching/linear-search.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
def linear_search(sequence, target):
3+
4+
for index, item in enumerate(sequence):
5+
if item == target:
6+
return index
7+
8+
return None
9+
10+
def main():
11+
print("=== Linear Search (Algorithm) - An efficient, general-purpose, comparison-based sorting algorithm which most implementations produce a stable sort. Merge-sort is a divide and conquer algorithm")
12+
user_input = raw_input("Enter numbers separated by a comma: ")
13+
sequence = [int(item) for item in user_input.split(',')]
14+
15+
target_input = input("Enter single number to be found in the list: ")
16+
target = int(target_input)
17+
18+
result = linear_search(sequence, target)
19+
20+
if result is not None:
21+
print("{} found at position: {}".format(target, result))
22+
else:
23+
print("Not found")
24+
25+
if __name__=='__main__':
26+
main()

0 commit comments

Comments
 (0)