Last active
January 9, 2016 02:17
-
-
Save giwa/9a63bb958fae6aaa093f to your computer and use it in GitHub Desktop.
visualize insertion sort ref: http://qiita.com/giwa/items/ef2811ce5003f2918eab
This file contains 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
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) | |
This file contains 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
[ 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