still futzing around with insertion_sort and timeit
This commit is contained in:
parent
88658c2a5d
commit
b5a69f4397
@ -2,7 +2,7 @@ from __future__ import print_function
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import unittest
|
import timeit
|
||||||
|
|
||||||
|
|
||||||
DEBUG = os.environ.get('DEBUG') == '1'
|
DEBUG = os.environ.get('DEBUG') == '1'
|
||||||
@ -32,26 +32,26 @@ def insertion_sort(arr):
|
|||||||
return arr
|
return arr
|
||||||
|
|
||||||
|
|
||||||
class TestStuff(unittest.TestCase):
|
def main():
|
||||||
|
for power in range(1, 5):
|
||||||
|
to_sort = list(range(10 ** power))
|
||||||
|
to_sort_sorted = to_sort[:]
|
||||||
|
to_sort_reversed = list(reversed(to_sort[:]))
|
||||||
|
|
||||||
def test_insertion_sort_small(self):
|
for i in range(3):
|
||||||
self.assertEqual(
|
random.shuffle(to_sort)
|
||||||
[1, 2, 3, 4, 5, 6],
|
|
||||||
insertion_sort([5, 2, 4, 6, 1, 3])
|
for arr, desc in (
|
||||||
|
(to_sort_sorted, 'sorted'),
|
||||||
|
(to_sort, 'shuffled'),
|
||||||
|
(to_sort_reversed, 'reversed')):
|
||||||
|
|
||||||
|
timing = timeit.timeit(
|
||||||
|
lambda: insertion_sort(arr),
|
||||||
|
number=10
|
||||||
)
|
)
|
||||||
|
print('{} {}: {}'.format(10 ** power, desc, timing))
|
||||||
def test_insertion_sort_large(self):
|
|
||||||
inlist = range(1000)
|
|
||||||
listcopy = inlist[:]
|
|
||||||
random.shuffle(inlist)
|
|
||||||
random.shuffle(inlist)
|
|
||||||
self.assertEqual(listcopy, insertion_sort(inlist))
|
|
||||||
|
|
||||||
def test_insertion_sort_large_reversed(self):
|
|
||||||
inlist = range(1000)
|
|
||||||
listcopy = inlist[:]
|
|
||||||
self.assertEqual(listcopy, insertion_sort(list(reversed(inlist))))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user