Skip to content

Commit 0f3d7c3

Browse files
Merge pull request souravjain540#121 from MeenakshyBS/patch-6
Create InsertionSort.py
2 parents 8846b68 + a318f08 commit 0f3d7c3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

InsertionSort.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def insertion_sort(arr):
2+
3+
for i in range(1, len(arr)):
4+
key = arr[i]
5+
j = i - 1
6+
7+
while j >= 0 and key < arr[j]:
8+
arr[j + 1] = arr[j]
9+
j = j - 1
10+
11+
12+
arr[j + 1] = key
13+
14+
arr = [9, 8, 6, 7, 1]
15+
print("Unsorted Array:", arr)
16+
insertion_sort(arr)
17+
print('Sorted Array: ', arr)

0 commit comments

Comments
 (0)