8000 Insertion Sort Added · AllAlgorithms/python@81ce442 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81ce442

Browse files
committed
Insertion Sort Added
1 parent 000beb1 commit 81ce442

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

sorting/insertion_sort.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Python program for implementation of Insertion Sort
2+
3+
# Function to do insertion sort
4+
def insertionSort(arr):
5+
6+
7+
for i in range(1, len(arr)):
8+
9+
key = arr[i]
10+
11+
j = i-1
12+
while j >=0 and key < arr[j] :
13+
arr[j+1] = arr[j]
14+
j -= 1
15+
arr[j+1] = key
16+
17+
18+
arr = [12, 11, 13, 5, 6]
19+
insertionSort(arr)
20+
for i in range(len(arr)):
21+
print ("%d" %arr[i])

0 commit comments

Comments
 (0)
0