Skip to content

Commit 2b0278d

Browse files
committed
Added insertion sort
1 parent 8714496 commit 2b0278d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pygorithms/sorting/insertion_sort.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Author: OMKAR PATHAK
2+
# Created On: 31st July 2017
3+
4+
# Best O(n); Average O(n^2); Worst O(n^2)
5+
6+
# insertion sort algorithm
7+
def sort(List):
8+
for i in range(1, len(List)):
9+
currentNumber = List[i]
10+
for j in range(i - 1, -1, -1):
11+
if List[j] > currentNumber :
12+
List[j], List[j + 1] = List[j + 1], List[j]
13+
else:
14+
List[j + 1] = currentNumber
15+
break
16+
return List
17+
18+
# time complexities
19+
def bestcase_complexity():
20+
return 'O(n)'
21+
22+
def averagecase_complexity():
23+
return 'O(n ^ 2)'
24+
25+
def worstcase_complexity():
26+
return 'O(n ^ 2)'
27+
28+
# easily retrieve the source code of the sort function
29+
def get_code():
30+
import inspect
31+
return inspect.getsource(sort)

0 commit comments

Comments
 (0)