Skip to content

Commit 5d94867

Browse files
authored
add Insertion sort.py
1 parent 3aa4f3f commit 5d94867

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)