This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
DSA/insertion_sort.py
2019-03-25 15:21:57 +01:00

22 lines
586 B
Python
Executable file

#!/usr/bin/env python3
# vim: set ts=4 sw=4 et tw=80:
import sys
def insertionsort(A):
# start with the first and the second
# insert the next element in the right place in the first elements
# continue until the array is done
for j in range(1,len(A)):
i = j - 1
# while swapping is necessary or at the end of array
while i >= 0 and A[i+1] < A[i]:
print(A)
# Swap A[i] and A[j]
A[i+1], A[i] = A[i], A[i+1]
i = i - 1
return A
args = [int(x) for x in sys.argv[1:]]
print(insertionsort(args))