def sortList(self, myList): if (self.sortingAlg == 'Bubble Sort'): bubbleSort(myList, delay=self.sleepTime) elif (self.sortingAlg == 'Merge Sort'): mergeSort(myList, delay=self.sleepTime) elif (self.sortingAlg == 'Quick Sort'): quickSort(myList, delay=self.sleepTime) self.buttons[1].setText('Start') self.sorting = False
def playtime(toys): print("\n==SUBMENU=======================================\n" + " (1) Name (Bubble Sort)\n" + " (2) Height (Merge Sort)\n" + " (3) Species (Insertion Sort)\n" + " (4) First Movie Appearance (Selection Sort)\n" + " (5) Number of Feet (Quick Sort)\n" + "================================================\n") command_check = False while command_check == False: #This loop checks if the input is valid command = int(input("Enter command number(1-5): ")) if command in range(1, 6): command_check = True if command == 1: toys = sort.BubbleSort(toys) elif command == 2: toys = sort.mergeSort(toys) elif command == 3: toys = sort.InsertionSort(toys) elif command == 4: toys = sort.SelectionSort(toys) elif command == 5: toys = sort.quickSort(toys, 0, len(toys) - 1) print(sort.display(toys)) return toys
def mergeSortTime(): SETUP_CODE = ''' import sort import random testList = [] for i in range(50000): testList.append(random.randint(0,1000))''' TEST_CODE = ''' sort.mergeSort(testList)''' times = timeit.repeat(setup=SETUP_CODE, stmt=TEST_CODE, repeat=1, number=1) print('mergeSortTime: {}'.format(min(times)))
# coding: utf-8 # 名前のリストのソート from sort import merge,mergeSort def lastNameFirstName(name1, name2): import string name1 = string.split(name1, ' ') name2 = string.split(name2, ' ') if name1[1] != name2[1]: return name1[1] < name2[1] else: #姓が同じであれば、名によりソート return name1[0] < name2[0] def firstNameLastName(name1, name2): import string name1 = string.split(name1, ' ') name2 = string.split(name2, ' ') if name1[0] != name2[0]: return name1[0] < name2[0] else: #名が同じであれば、姓によりソート return name1[1] < name2[1] L = ['Chris Terman', 'Tom Brady', 'Eric Grimson', 'Gisele Bundchen'] newL = mergeSort(L, lastNameFirstName) print 'Sorted by last name =', newL newL = mergeSort(L, firstNameLastName) print 'Sorted by first name =', newL
def add(self,newBall): self.elements.append(newBall) self.elements = sort.mergeSort(self.elements)
#! /etc/bin/python3 import sort List1 = [1,2,46,26,357,15] result1 = sort.InsertSort(List1) print(result1) result2 = sort.mergeSort(List1) print(result2) result3 = sort.bucketSort(List1) print(result3) result4 = sort.quickSort(List1,0,len(List1)-1) print(result4)
print(testList) else: testList = [] for i in range(int(sys.argv[2])): testList.append(random.randint(0, 10000)) sort.insertionSort(testList) print("LIST SORTED") elif sys.argv[1] == "MergeSort": if len(sys.argv) > 3: if sys.argv[3] == "PRINT": testList = [] for i in range(int(sys.argv[2])): testList.append(random.randint(0, 10000)) print(testList, "\n") sort.mergeSort(testList) print(testList) else: testList = [] for i in range(int(sys.argv[2])): testList.append(random.randint(0, 10000)) sort.mergeSort(testList) print("LIST SORTED") elif sys.argv[1] == "IterativeMergeSort": if len(sys.argv) > 3: if sys.argv[3] == "PRINT": testList = [] for i in range(int(sys.argv[2])): testList.append(random.randint(0, 10000)) print(testList, "\n")
import sort A = [4, 3, 6, 4, 5, 8, 5, 67, 3, 5, 3 , 74 , 0, 6] sort.mergeSort(A, 0, len(A) - 1) print(A)
import random import sort LIST_SIZE = 100 l = [] for i in range(LIST_SIZE): randNumber = random.randint(0, 10 * LIST_SIZE) l.append(randNumber) print(l) sort.mergeSort(l) print(l)
if temp != baseData: print("Insertion Sort issue") print("Insertion sort takes on average {:.5f}".format((time.clock()-start)/num)) start = time.clock() for i in range(num): temp = copy.copy(res[i]) quickSort(temp) if temp != baseData: print("Quick Sort issue") print("Quick sort takes on average {:5f}".format((time.clock()-start)/num)) start = time.clock() for i in range(num): temp = copy.copy(res[i]) rquickSort(temp) if temp != baseData: print("Random Quick Sort issue") print("Random Quick sort takes on average {:5f}".format( (time.clock()-start)/num)) start = time.clock() for i in range(num): temp = copy.copy(res[i]) mergeSort(temp) if temp != baseData: print("Merge Sort issue") print("Merge sort takes on average {:5f}".format((time.clock()-start)/num))
from tqdm import tqdm import numpy as np if __name__ == '__main__': print('also look here: ', __name__) MIN = 0 MAX = 1000 bubble_sort_mean = [] merge_sort_mean = [] bubble_sort_std_dev = [] merge_sort_std_dev = [] arr_sizes = [] for i in tqdm(range(5, 1000, 100)): sample = random.choices(range(MIN, MAX), k=i) thing = bubbleSort(sample, timeme=True) thing = mergeSort(sample, timeme=True) experiment = Timer.records['sorting_experiment'] arr_sizes.append(int(i)) bubble_sort_mean.append(experiment['bubbleSort'][-1].aggregate['mean']) bubble_sort_std_dev.append( experiment['bubbleSort'][-1].aggregate['std_dev']) merge_sort_mean.append(experiment['mergeSort'][-1].aggregate['mean']) merge_sort_std_dev.append( experiment['mergeSort'][-1].aggregate['std_dev']) bubble_sort_mean = np.array(bubble_sort_mean) bubble_sort_std_dev = np.array(bubble_sort_std_dev) merge_sort_mean = np.array(merge_sort_mean) merge_sort_std_dev = np.array(merge_sort_std_dev) plt.plot(arr_sizes, bubble_sort_mean,
# -*- coding:utf-8 -*- import random from sort import (bubbleSort, bubbleSort2, selectionSort, insertSort, shellSort, shellSort2, mergeSort, quickSort, quickSort2) if __name__ == '__main__': test_list = [random.randrange(0, 1000) for i in range(100000)] # test_list = [305, 456, 833, 758, 348, 370, 416, 333, 356, 19] test_list # 复制一份test_list 传给排序函数,可使用list, [:]或copy方法 # print bubbleSort(list(test_list)) # print bubbleSort2(list(test_list)) # print selectionSort(list(test_list)) # print insertSort(list(test_list)) # print shellSort(list(test_list)) mergeSort(list(test_list)) quickSort(list(test_list)) quickSort2(list(test_list))
################ Sorting the array ################ print('\nSorting in progress...') sorts, times, comparisons, accesses = [], [], [], [] for i in range(tries): startTime = time() if algorithm == '1': sorted_array, comparison, access = sort.someSort(unsorted_array) elif algorithm == '2': sorted_array, comparison, access = sort.insertionSort(unsorted_array) elif algorithm == '3': sorted_array, comparison, access = sort.selectionSort(unsorted_array) elif algorithm == '4': sorted_array, comparison, access = sort.bubbleSort(unsorted_array) elif algorithm == '5': sorted_array, comparison, access = sort.mergeSort(unsorted_array) elif algorithm == '6': sorted_array, comparison, access = sort.bogoSort(unsorted_array) times.append(time() - startTime) sorts.append(sorted(unsorted_array) == sorted_array) comparisons.append(comparison) accesses.append(access) ################################################### ################ Analyze test data ################ print('Sorting finished!\n') print('Length of array: %d' % len(unsorted_array)) print('Sorted: %d/%d (%d%%)' % (sorts.count(True), tries, sorts.count(True) / tries * 100)) print('Most Comparisons: %d' % max(comparisons)) print('Least Comparisons: %d' % min(comparisons))
insertSort(temp) if temp != baseData: print("Insertion Sort issue") print("Insertion sort takes on average {:.5f}".format( (time.clock() - start) / num)) start = time.clock() for i in range(num): temp = copy.copy(res[i]) quickSort(temp) if temp != baseData: print("Quick Sort issue") print("Quick sort takes on average {:5f}".format((time.clock() - start) / num)) start = time.clock() for i in range(num): temp = copy.copy(res[i]) rquickSort(temp) if temp != baseData: print("Random Quick Sort issue") print("Random Quick sort takes on average {:5f}".format( (time.clock() - start) / num)) start = time.clock() for i in range(num): temp = copy.copy(res[i]) mergeSort(temp) if temp != baseData: print("Merge Sort issue") print("Merge sort takes on average {:5f}".format((time.clock() - start) / num))