Created
January 31, 2013 05:25
-
-
Save nnabeyang/4680483 to your computer and use it in GitHub Desktop.
シェルソート
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 shell_sort(a): | |
| size = len(a) | |
| h = 1 | |
| while True: | |
| h = 3 * h + 1 | |
| if h > size: break | |
| while True: | |
| h /= 3 | |
| #print "h=%d" % h | |
| for i in range(h, size): | |
| v = a[i] | |
| j = i | |
| while j >= h and v < a[j - h]: | |
| a[j] = a[j - h] | |
| j -= h | |
| if i != j: a[j] = v | |
| #print "a=%s" % a | |
| if h == 1: break | |
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
| #!/usr/bin/env python2.7 | |
| from test import test_support | |
| import unittest | |
| import random | |
| from shell_sort import * | |
| class TestClass(unittest.TestCase): | |
| def test_sort(self): | |
| for i in range(10000): | |
| origine = range(random.randint(2, 100)) | |
| a = origine[:] | |
| while a == origine: random.shuffle(a) | |
| self.assertNotEquals(origine, a) | |
| shell_sort(a) | |
| self.assertEquals(origine, a) | |
| test_support.run_unittest(TestClass) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment