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

24 lines
567 B
Python
Raw Normal View History

2019-02-28 12:29:34 +00:00
#!/usr/bin/env python3
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
tomove = A[j]
while i >= 0 and tomove < A[i]:
if A[i] > A[i+1]:
# 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))