Skip to content

Instantly share code, notes, and snippets.

@pinglunliao
Created May 26, 2017 02:49
Show Gist options
  • Save pinglunliao/3424af46f61c6f8a7fbe9a8e5697dc71 to your computer and use it in GitHub Desktop.
Save pinglunliao/3424af46f61c6f8a7fbe9a8e5697dc71 to your computer and use it in GitHub Desktop.
import random;
unsortData = random.sample(range(100), 10)
def insertion_sort(lst):
if len(lst) == 1:
return lst
for i in range(1, len(lst)):
temp = lst[i]
j = i - 1
while j >= 0 and temp < lst[j]:
lst[j + 1] = lst[j]
j -= 1
lst[j + 1] = temp
return lst
print "Original Data:", unsortData
sortData = insertion_sort(unsortData);
print "Sorted Data:", sortData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment