We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8714496 commit 2b0278dCopy full SHA for 2b0278d
pygorithms/sorting/insertion_sort.py
@@ -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
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