Skip to content

Commit e9a5866

Browse files
committed
Added Liear Search
1 parent 20a2566 commit e9a5866

23 files changed

+29
-0
lines changed
File renamed without changes.

pygorithm/searching/linear_search.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Author: OMKAR PATHAK
2+
# Created On: 1st August 2017
3+
4+
# Best O(1); Average O(n); Worst O(n)
5+
6+
# sequential search algorithm
7+
def search(List, target):
8+
'''This function returns the position of the target if found else returns -1'''
9+
position = 0
10+
while position < len(List):
11+
if target == List[position]:
12+
return position
13+
position += 1
14+
return -1
15+
16+
# time complexities
17+
def bestcase_complexity():
18+
return 'O(1)'
19+
20+
def averagecase_complexity():
21+
return 'O(n)'
22+
23+
def worstcase_complexity():
24+
return 'O(n)'
25+
26+
# easily retrieve the source code of the sort function
27+
def get_code():
28+
import inspect
29+
return inspect.getsource(search)
File renamed without changes.

0 commit comments

Comments
 (0)