def get_comparisons(pivot_func): with open('QuickSort.txt') as f: array = [int(l) for l in f.readlines()] qsort = QuickSort(pivot_func=pivot_func) comparisons, sorted_array = qsort.sort(array) # Validate sort result assert all(sorted_array[i] == el for i, el in enumerate(sorted(array))) return comparisons
class TestQuickSort(unittest.TestCase): """ quicksort 的测试用例 """ def setUp(self): self.qs = QuickSort() def tearDown(self): self.qs = None def testCommon(self): assert self.qs.sort([2, 4, 1, 3]) == [1, 2, 3, 4] def testBoundary(self): assert self.qs.sort([]) == [] def testSingle(self): assert self.qs.sort([1,]) == [1,] def testEqu(self): assert self.qs.sort([1, 1]) == [1, 1] def testNeg(self): assert self.qs.sort([1, -1]) == [-1, 1] def testRandom(self): data = [random.randint(1,1000) for i in range(1,1000)] qdata = data[:] data.sort() assert self.qs.sort(qdata) == data
from bubblesort import BubbleSort from insertionsort import InsertionSort from selectionsort import SelectionSort from mergesort import MergeSort from quicksort import QuickSort def isSorted(x, key=lambda x: x): return all([key(x[i]) <= key(x[i + 1]) for i in xrange(len(x) - 1)]) if __name__ == "__main__": to_sort = [613, 55, 8721, 472, 94, 72, 74, 8, 61, 356] _sorted = [8, 55, 61, 72, 74, 94, 356, 472, 613, 8721] #BubbleSort.sort(to_sort) #InsertionSort.sort(to_sort) #SelectionSort.sort(to_sort) #MergeSort.sort(to_sort) QuickSort.sort(to_sort) print to_sort assert isSorted(to_sort)