def test_string_sort_iterative(self): a_list = [ "this", "is", "some", "sample", "data", "for", "sorting", "test" ] b_list = a_list.copy() merge_sort(a_list, 'iterative') b_list.sort() self.assertEqual(a_list, b_list)
def median(list): if len(list) % 2 == 0: merged = merge_sort(list) mid = int(len(merged) / 2) - 1 avg = int(sum(merged[mid:mid + 2]) / 2) return avg else: return merge_sort(list)[int(len(list) / 2)]
def test_merge_sort(self): self.assertEqual([1, 5, 23, 57, 65, 1232], merge_sort([1, 5, 65, 23, 57, 1232]))
from sort.selection_sort import selection_sort from sort.insertion_sort import insertion_sort from sort.merge_sort import merge_sort from sort.quick_sort import quick_sort from sort.shell_sort import shell_sort data = [2, 3, 1, 9, 6, 4, 5, 7, 8] print("Lista original:", data) bubble_sort(data) print("Ordenado con burbuja clasica", data) data = [2, 3, 1, 9, 6, 4, 5, 7, 8] selection_sort(data) print("Ordenado con selection sort:", data) data = [2, 3, 1, 9, 6, 4, 5, 7, 8] insertion_sort(data) print("Ordenado con insertion sort:", data) data = [2, 3, 1, 9, 6, 4, 5, 7, 8] merge_sort(data) print("Ordenado con MergeSort:", data) data = [2, 3, 1, 9, 6, 4, 5, 7, 8] quick_sort(data, 0, len(data) - 1) print("Ordenando con Quicksort:", data) data = [2, 3, 1, 9, 6, 4, 5, 7, 8] shell_sort(data) print("Ordenando con Shellsort:", data)
def test_integer_sort_iterative(self): a_list = list(range(100)) shuffle(a_list) merge_sort(a_list, 'iterative') self.assertEqual(a_list, list(range(100)))
from sort.bubble_sort import bubble_sort from sort.heap_sort import heap_basic_sort from sort.shell_sort import shell_sort from sort.quick_sort import quick_sort # 随机数组 length = 10000 array = [random.randint(0, length - 1) for i in range(length)] print("data length: {}".format(length)) start = time.clock() insert_sort(copy.deepcopy(array)) print("insert sort cost {}s".format(time.clock() - start)) start = time.clock() merge_sort(copy.deepcopy(array)) print("merge sort cost {}s".format(time.clock() - start)) start = time.clock() bubble_sort(copy.deepcopy(array)) print("bubble sort cost {}s".format(time.clock() - start)) start = time.clock() heap_basic_sort(copy.deepcopy(array)) print("heap sort cost {}s".format(time.clock() - start)) start = time.clock() shell_sort(copy.copy(array)) print("shell sort cost {}s".format(time.clock() - start)) start = time.clock()
def readFilesAndSort(filenameToSave, algorithm_choice): filename = filenameToSave + '.csv' unsorted_csv = open(filename, "r+") reader = csv.reader(unsorted_csv) data = [] start_time = time.time() unsorted_csv.readline() for row in reader: data.append([(float)(row[3]), (row[0]), (float)(row[1]), (float)(row[2])]) if algorithm_choice == 1: merge_sort.merge_sort(data) end_time = time.time() - start_time fieldnames_for_csv = [ 'item_description', 'item_price', 'item_shipping', 'total_price' ] sorted_csv_data = open(filename + "_mergesort.csv", "w+") sorted_data_writer = csv.DictWriter(sorted_csv_data, fieldnames=fieldnames_for_csv) sorted_data_writer.writeheader() for items in data: sorted_data_writer.writerow({ 'item_description': items[1], 'item_price': items[2], 'item_shipping': items[3], "total_price": items[0] }) sorted_csv_data.close() return end_time if algorithm_choice == 2: data = quick_sort.quick_sort(data) end_time = time.time() - start_time fieldnames_for_csv = [ 'item_description', 'item_price', 'item_shipping', 'total_price' ] sorted_csv_data = open(filename + "_quicksort.csv", "w+") sorted_data_writer = csv.DictWriter(sorted_csv_data, fieldnames=fieldnames_for_csv) sorted_data_writer.writeheader() for items in data: sorted_data_writer.writerow({ 'item_description': items[1], 'item_price': items[2], 'item_shipping': items[3], "total_price": items[0] }) sorted_csv_data.close() return end_time if algorithm_choice == 3: heap_sort.heap_sort(data) end_time = time.time() - start_time fieldnames_for_csv = [ 'item_description', 'item_price', 'item_shipping', 'total_price' ] sorted_csv_data = open(filename + "_heapsort.csv", "w+") sorted_data_writer = csv.DictWriter(sorted_csv_data, fieldnames=fieldnames_for_csv) sorted_data_writer.writeheader() for items in data: sorted_data_writer.writerow({ 'item_description': items[1], 'item_price': items[2], 'item_shipping': items[3], "total_price": items[0] }) sorted_csv_data.close() return end_time
# bubble_sort for max_weight bubble_execution_start = time.time() bubble_sort(domcrats_max_weight) bubble_execution_end = time.time() bubble_execution_time = bubble_execution_end - bubble_execution_start print("\nBubble sorted array of max_weight (execution time: %f)" % bubble_execution_time) print("Comparisons: {}\nSwaps: {}".format(bubble_sort.comparisones, bubble_sort.swaps)) for i in range(len(domcrats_max_weight)): print(domcrats_max_weight[i]) # mergre_sort for weight merge_execution_start = time.time() merge_sort(domcrats_weight) merge_execution_end = time.time() merge_execution_time = merge_execution_end - merge_execution_start print("\nMerge sorted array of weight (execution time: %f)" % merge_execution_time) print("Comparisons: {}\nSwaps: {}".format(merge_sort.comparisones, merge_sort.swaps)) for i in range(len(domcrats_weight)): print(domcrats_weight[i]) # write data to CSV file write_array = [] write_array.append("Bubble sort") write_array.append(bubble_sort.comparisones)
def test_merge_sort(sort_test_cases, test_num): items = sort_test_cases(test_num) sorted_items = merge_sort(items) assert sorted_items == sorted(items)