Skip to content

Commit c7c62e4

Browse files
Insertion Sort in Python
1 parent f69598b commit c7c62e4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

sorting/insertion-sort.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of DSA course available on CourseGalaxy.com
3+
4+
def insertion_sort(a):
5+
for i in range(1,len(a)):
6+
temp = a[i]
7+
j = i-1
8+
while j >= 0 and a[j] > temp:
9+
a[j+1] = a[j]
10+
j=j-1
11+
a[j+1] = temp
12+
13+
####################################
14+
15+
list1 = [6,3,1,5,9,8]
16+
insertion_sort(list1)
17+
print(list1)
18+
19+
list2 = [2,3,5,39,11,8,9,166,45,23]
20+
insertion_sort(list2)
21+
print(list2)
22+
23+
list3 = [1,2,3,4,5,6,7,8,9,10]
24+
insertion_sort(list3)
25+
print(list3)
26+
27+
list4 = [10,9,8,7,6,5,4,3,2,1]
28+
insertion_sort(list4)
29+
print(list4)
30+
31+
list5 = [4]
32+
insertion_sort(list5)
33+
print(list5)
34+
35+
#####################################

0 commit comments

Comments
 (0)