def main(): numbers = [] if (len(sys.argv) == 1): for line in sys.stdin: line = line.strip('\n') numbers = line.split(' ') numbers = [float(i) for i in numbers] else: CLI = argparse.ArgumentParser() CLI.add_argument( "numbers", nargs='*', type=float, default=None, ) args = CLI.parse_args() numbers = args.numbers """ print('Loading numbers...') bubble.sort(numbers.copy()) print('\nLoading numbers...') merge.sort(numbers.copy()) print('\nLoading numbers...') insertion.sort(numbers.copy()) print('\nLoading numbers...') """ quick.sort(numbers.copy()) """
def test_kitchen_sink(self): self.assertEqual( sort([1,2,5,72,3,6,3,234,5,6,4,2,1,7,9,6,4,322,7,56,23,4,6,4,2,3,6,8,5,3,1,3]), [1,1,1,2,2,2,3,3,3,3,3,4,4,4,4,5,5,5,6,6,6,6,6,7,7,8,9,23,56,72,234,322] ) self.assertEqual( sort([9,5,4,3,1,6,5,7,5,8,9,9,8,0,8,9,8,7,4,2,1,5,4,6,7,3,1,4,3,5,7,8,9,3]), [0,1,1,1,2,3,3,3,3,4,4,4,4,5,5,5,5,5,6,6,7,7,7,7,8,8,8,8,8,9,9,9,9,9] )
def test_fixed_seed(self): def is_sorted(A): for i in range(1, len(A)): if A[i-1] > A[i]: return False return True import random random.seed(17) for i in range(100): A = random.sample(xrange(10000000), 10000) self.assertTrue(is_sorted(sort(A)))
def test_sort(self): tests = ( ( (4, 1, 6, 5, 7, 8, 14, 13, 12, 10, 11, 2, 3, 9), (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) ), ( (10, 9, 8, 7, 6, 5, 4, 3, 2, 1), (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) ), ( (1), (1) ) ) for notSorted, nowSorted in tests: self.assertEqual(sort(notSorted), nowSorted)
def test_reversed(self): self.actual.reverse() quick.sort(self.actual) self.assertEqual(list(sorted(self.actual)), self.actual)
def test_oneoff(self): self.actual[0], self.actual[1] = self.actual[1], self.actual[0] quick.sort(self.actual) self.assertEqual(list(sorted(self.actual)), self.actual)
def test_sorted(self): quick.sort(self.actual) self.assertEqual(list(sorted(self.actual)), self.actual)
def test_random(self): random.shuffle(self.actual) quick.sort(self.actual) self.assertEqual(list(sorted(self.actual)), self.actual)
def test_single(self): self.assertEqual(sort([1]), [1]) self.assertEqual(sort([0]), [0]) self.assertEqual(sort([-1]), [-1])
def test_empty(self): self.assertEqual(sort([]), [])
def test_sorted(self): self.assertEqual(sort([1]), [1]) self.assertEqual(sort([1,2]), [1,2]) self.assertEqual(sort([1,2,3]), [1,2,3]) self.assertEqual(sort([1,2,3,4]), [1,2,3,4])
def test_multiples(self): self.assertEqual(sort([1,1]), [1,1]) self.assertEqual(sort([5,5,5,5]), [5,5,5,5]) self.assertEqual(sort([5,5,4,5]), [4,5,5,5])
def test_two(self): self.assertEqual(sort([2,1]), [1,2]) self.assertEqual(sort([1,2]), [1,2]) self.assertEqual(sort([7,8]), [7,8]) self.assertEqual(sort([7,4]), [4,7])
def test_quicksort(self): source = randnum.rand(50) target = copy.deepcopy(source) quick.sort(source) self.assertEqual(source, sorted(target))
start = timeit.default_timer() heap.sort(data) end = timeit.default_timer() summary.write(str((end-start)*1000) + "\n") total += (end-start)*1000 summary.write("Avg: " + str(total/jobsLimit) + "\n\n") # QuickSort summary.write("[QuickSort]\n") for variable in N: summary.write("> " + str(variable) + " variable: \n") total = 0 for job in range(jobsLimit): data = randnum.rand(variable) start = timeit.default_timer() quick.sort(data) end = timeit.default_timer() summary.write(str((end-start)*1000) + "\n") total += (end-start)*1000 summary.write("Avg: " + str(total/jobsLimit) + "\n\n") # Build-In Sort (TimSort) summary.write("[TimSort(Build-In)]\n") for variable in N: summary.write("> " + str(variable) + " variable: \n") total = 0 for job in range(jobsLimit): data = randnum.rand(variable) start = timeit.default_timer() data.sort() end = timeit.default_timer()