def test_empty_array(self): sorted_arr = heapsort.sort(numpy.empty(0)) try: numpy.testing.assert_array_equal(sorted_arr, []) self.assertTrue(True) except AssertionError as error: self.assertTrue(False, error)
def test_one_element_array(self): sorted_arr = heapsort.sort(numpy.array([2])) try: numpy.testing.assert_array_equal(sorted_arr, numpy.array([2])) self.assertTrue(True) except AssertionError as error: self.assertTrue(False, error)
def test_small_array(self): sorted_arr = heapsort.sort(numpy.array([4, 8, 3, 9, 0, 8])) try: numpy.testing.assert_array_equal(sorted_arr, numpy.array([0, 3, 4, 8, 8, 9])) self.assertTrue(True) except AssertionError as error: self.assertTrue(False, error)
def sort(method, isSorted, i): # Tuple that is returned from the getDB() method # ITEM_DATABASE is a dictionary where the keys are integers and the values are an array # upc14_collection is an array of integers ITEM_DATABASE, upc14_collection = getDB(isSorted) while i < len(upc14_collection): if i == 0: break else: del upc14_collection[i] # Tracks the amount of time it takes for the algorithm to run start_time = time.clock() # Checks to see which method the user wants to use if method == 'QUICKSORT': quicksort.sort(upc14_collection) elif method == 'RADIXSORT': radixsort.sort(upc14_collection) elif method == 'HEAPSORT': heapsort.sort(upc14_collection) # Ends the time when the algorithm is finished end_time = time.clock() # Prints the algorithm used and how long it took to compute clock = end_time - start_time print('{} DONE: {} seconds'.format(method, clock)) #Opens the output sorted file and writes to the file to enter the newly sorted data with open('Sorted Database.csv', 'w') as file: #For each upc code, a new line in the file will be written for i in range(0, len(upc14_collection)): #Written in the format: UPC14 CODE, BRAND, PRODUCT NAME file.write('{}, {}, {} \n'.format( ITEM_DATABASE.get(upc14_collection[i])[0], ITEM_DATABASE.get(upc14_collection[i])[1], ITEM_DATABASE.get(upc14_collection[i])[2])) file.close() # OS opens the sorted text file os.system('open Sorted\ Database.csv')
def testSingleElem(self): A = [1] heapsort.sort(A) self.assertEqual(A, [1])
def testGeneralCase(self): A = [3,2,6,5,1,8,7,9,4,10] heapsort.sort(A) self.assertEqual(A, [1,2,3,4,5,6,7,8,9,10])
def random_list(list): size = len(list) for i in range(1, size): r1 = rn(size) r2 = rn(size) heapsort.swap(list, r1, r2) list = [1, 2, 8, 4, 7, 0, 9, 6, 3, 5] qsortlist = list.copy() simpleselectsortlist = list.copy() bubblesortlist = list.copy() mergesortlist = list.copy() heapsort.sort(list) print(list) list = qsortlist qsort.sort(list) print(list) list = simpleselectsortlist simpleselectsort.sort(list) print(list) bubblesort.sort(bubblesortlist) print(bubblesortlist) mergesort.sort(mergesortlist) print(mergesortlist)
def testGeneralCase(self): A = [3, 2, 6, 5, 1, 8, 7, 9, 4, 10] heapsort.sort(A) self.assertEqual(A, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
def test_heapsort(self): heapsort.sort(self.test) self.assertItemsEqual(self.test, self.expected)