Last active
December 24, 2015 05:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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