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 3aa4f3f commit 5d94867Copy full SHA for 5d94867
classical_algorithms/python/Insertion Sort.py
@@ -0,0 +1,26 @@
1
+def insertion_sort(array):
2
+
3
+ for i in range(1,len(array)):
4
+ j = i-1
5
+ key = array[i]
6
7
+ #move all the elements of array[0,i-1] that are greater than the key, one position ahead of their current one
8
9
+ while j>=0 and array[j]>key:
10
+ array[j+1] = array[j]
11
+ j = j-1
12
13
+ array[j+1] = key
14
15
+ print(array)
16
17
18
+m = int(input('Enter the number of elements in the array '))
19
20
+mylist = []
21
22
+for i in range(0,m):
23
+ num = int(input('Enter element: '))
24
+ mylist.append(num)
25
26
+insertion_sort(mylist)
0 commit comments