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-05 13:23:46 +01:00

23 lines
599 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)):
print(A)
print(A)
i = j - 1
# while swapping is necessary or at the end of array
while i >= 0 and A[i+1] < A[i]:
# 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))