Skip to content

Instantly share code, notes, and snippets.

@figengungor
Last active December 24, 2015 05:39
Show Gist options
  • Select an option

  • Save figengungor/6752080 to your computer and use it in GitHub Desktop.

Select an option

Save figengungor/6752080 to your computer and use it in GitHub Desktop.
A function that takes an arbitrary number of arguments, sorts them using Insertion Sort and returns a list of sorted items.
def insertion_sort(*args):
sList=list(args[:]) //copies args to sList
for index in range(1, len(sList)): //starts from first index not zero
value = sList[index]
i = index-1
while i>=0: //compare values before the current value to the current value
if value<sList[i]:
sList[i+1], sList[i] = sList[i], sList[i+1] //swap if the current value smaller than previous value
i = i-1
else:
break
return sList
print(insertion_sort(5,4,3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment