Skip to content

Commit 9b02323

Browse files
authored
Added implementation of Sorting algorithms
1 parent fc51dad commit 9b02323

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
def insertionSort(Array):
3+
for i in range(1, len(Array)):
4+
key = Array[i]
5+
j = i - 1
6+
7+
# Compare key with each element on the left of it until an element
8+
# smaller than it is found
9+
# For descending order, change key<Array[j] to key>Array[j].
10+
while j >= 0 and key < Array[j]:
11+
Array[j + 1] = Array[j]
12+
j -= 1
13+
Array[j + 1] = key
14+
return Array
15+
16+
if __name__ == '__main__':
17+
Array = [-2, -3, -1, 11, 9, 12, 4, -5, -12, 6, 19, 20]
18+
print(insertionSort(Array))

0 commit comments

Comments
 (0)