Skip to content

Instantly share code, notes, and snippets.

@giwa
Last active January 9, 2016 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giwa/9a63bb958fae6aaa093f to your computer and use it in GitHub Desktop.
Save giwa/9a63bb958fae6aaa093f to your computer and use it in GitHub Desktop.
visualize insertion sort ref: http://qiita.com/giwa/items/ef2811ce5003f2918eab
import random
def vs_insertion(l, i):
n = l.copy()
n.insert(i+1, "*")
n.insert(i+1, "]")
n.insert(0, "[")
print(" ".join(map(lambda x: str(x), n)))
def insertion_sort(l):
size = len(l)
i = 1
while i < size:
tmp = l[i]
j = i - 1
while j >= 0 and tmp < l[j]:
l[j + 1] = l[j]
j -= 1
l[j + 1] = tmp
vs_insertion(l, i)
i += 1
r = [random.choice([i for i in range(1000)]) for r in range(10)]
insertion_sort(r)
print(r)
[ 169 170 ] * 579 477 707 108 884 393 172 447
[ 169 170 579 ] * 477 707 108 884 393 172 447
[ 169 170 477 579 ] * 707 108 884 393 172 447
[ 169 170 477 579 707 ] * 108 884 393 172 447
[ 108 169 170 477 579 707 ] * 884 393 172 447
[ 108 169 170 477 579 707 884 ] * 393 172 447
[ 108 169 170 393 477 579 707 884 ] * 172 447
[ 108 169 170 172 393 477 579 707 884 ] * 447
[ 108 169 170 172 393 447 477 579 707 884 ] *
[108, 169, 170, 172, 393, 447, 477, 579, 707, 884]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment