Skip to content

Commit bad9a04

Browse files
author
Souldiv
committed
Added insertion sort Python implementation
1 parent 1ebf8b7 commit bad9a04

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Sorting/Insertion_sort.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# optimized - shifts instead of swapping
2+
def insertion_sort(Arr):
3+
for i in range(1, len(Arr)):
4+
curNum = Arr[i]
5+
pos = 0
6+
for j in range(i-1, -2, -1):
7+
pos = j
8+
if Arr[j] > curNum:
9+
Arr[j+1] = Arr[j]
10+
else:
11+
break
12+
Arr[pos+1] = curNum
13+
return Arr
14+
15+
if __name__=="__main__":
16+
test = [10,67,2,998,23,56,32,21,91,21,22]
17+
print(insertion_sort(test))
18+

0 commit comments

Comments
 (0)