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 f8f3bc2 commit 80c598dCopy full SHA for 80c598d
classical_algorithms/python/Insertion Sort.py
@@ -0,0 +1,27 @@
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
8
+ #the key, one position ahead of their current one
9
10
+ while j>=0 and array[j]>key:
11
+ array[j+1] = array[j]
12
+ j = j-1
13
14
+ array[j+1] = key
15
16
+ print(array)
17
18
19
+m = int(input('Enter the number of elements in the array '))
20
21
+mylist = []
22
23
+for i in range(0,m):
24
+ num = int(input('Enter element: '))
25
+ mylist.append(num)
26
27
+insertion_sort(mylist)
0 commit comments