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

23 lines
586 B
Python
Raw Normal View History

2019-02-28 12:29:34 +00:00
#!/usr/bin/env python3
2019-03-05 12:23:46 +00:00
# vim: set ts=4 sw=4 et tw=80:
2019-02-28 12:29:34 +00:00
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
2019-03-05 12:23:46 +00:00
# while swapping is necessary or at the end of array
while i >= 0 and A[i+1] < A[i]:
2019-03-25 14:21:57 +00:00
print(A)
2019-03-05 12:23:46 +00:00
# Swap A[i] and A[j]
A[i+1], A[i] = A[i], A[i+1]
2019-02-28 12:29:34 +00:00
i = i - 1
return A
args = [int(x) for x in sys.argv[1:]]
print(insertionsort(args))