Skip to content

Commit 80c598d

Browse files
authored
add insertion_sort.py
1 parent f8f3bc2 commit 80c598d

File tree

1 file changed

+27
-0
lines changed

1 file changed

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

Comments
 (0)