def isPerm(a, b): if (len(a) != len(b)): return False sortedA = ms.mergeSort(a) sortedB = ms.mergeSort(b) for i in range(0, len(a)): if (sortedA[i] != sortedB[i]): return False return True
def main(): l = randomList() print("List size: ", len(l)) #BubbleSort start = time.time() BubbleSort.bubbleSort(l) end = time.time() print("BubbleSort: ", end - start) #InsertionSort start = time.time() InsertionSort.insertionSort(l) end = time.time() print("InsertionSort: ", end - start) #SelectionSort start = time.time() SelectionSort.selectionSort(l) end = time.time() print("SelectionSort: ", end - start) #ShellSort start = time.time() ShellSort.shellSort(l) end = time.time() print("ShellSort: ", end - start) #MergeSort start = time.time() MergeSort.mergeSort(l) end = time.time() print("MergeSort: ", end - start) #QuickSort start = time.time() QuickSort.quickSort(l, 0, len(l) - 1) end = time.time() print("QuickSort: ", end - start)
def test(numElements, algo): A = [] for ele in range(0, numElements): A.append(random.randint(0, 100000)) if algo == "MergeSort": B = MergeSort.mergeSort(A) elif algo == "InsertionSort": B = InsertionSort.InsertionSort(A) isSorted = True for ele in range(0, numElements - 1): if B[ele] > B[ele + 1]: isSorted = False return isSorted
def test(name, number): A = [] for n in range(100, 10100, 100): for k in range(0, number): array = np.random.randint(0, 1000, n) array1 = array.copy() array2 = array.copy() array3 = array.copy() array4 = array.copy() array5 = array.copy() array6 = array.copy() start = time.time() comparing, changes = IS.insertionSort(array1) end = time.time() timer = end - start data = { "Tablica": n, "Czas": timer, "Zmiany": changes, "Porownania": comparing, "Nazwa": "Insert" } A.append(data) start = time.time() comparing, changes = MS.mergeSort(array2) end = time.time() timer = end - start data = { "Tablica": n, "Czas": timer, "Zmiany": changes, "Porownania": comparing, "Nazwa": "Merge" } A.append(data) start = time.time() comparing, changes = QS.quickSort(array3, 0, len(array3) - 1) end = time.time() timer = end - start data = { "Tablica": n, "Czas": timer, "Zmiany": changes, "Porownania": comparing, "Nazwa": "Quick" } A.append(data) start = time.time() comparing, changes = H.megreInsertSort(array4) end = time.time() timer = end - start data = { "Tablica": n, "Czas": timer, "Zmiany": changes, "Porownania": comparing, "Nazwa": "Hibrid10" } A.append(data) start = time.time() comparing, changes = DP.DualPivot(array, 0, len(array5) - 1) end = time.time() timer = end - start data = { "Tablica": n, "Czas": timer, "Zmiany": changes, "Porownania": comparing, "Nazwa": "Dual" } A.append(data) try: file = open(name, "w") json.dump(A, file, indent=3) except IOError: pass finally: file.close()
# 测试所有的排序算法的效率 import random import sys import BubbleSort import InsertSort import MergeSort import QuickSort import SelectionSort import ShellSort import threading import copy if __name__ == '__main__': # 快速排序递归可能溢出,这里设置大一点的值 # sys.setrecursionlimit(1000000) ls = [i for i in range(1, 10001)] random.shuffle(ls) # 复制,否则前面的排序会影响到后面的计算 ls1 = copy.copy(ls) ls2 = copy.copy(ls) ls3 = copy.copy(ls) ls4 = copy.copy(ls) ls5 = copy.copy(ls) # 挨个运行 BubbleSort.bubbleSort(ls) SelectionSort.selectionSort(ls1) InsertSort.insertSort(ls2) ShellSort.shellSort(ls3) MergeSort.mergeSort(ls4) QuickSort.quickSort(ls5)
import QuickSort import MergeSort import InsertionSort import SelectionSort if __name__ == '__main__': l = [2, 7, 3, 1, 0, 11, 9, 8] QuickSort.quickSort(l) MergeSort.mergeSort(l) InsertionSort.insertionSort(l) SelectionSort.selectionSort(l) print(l)
def mergeSortUnique(s): sortedList = ms.mergeSort(s) for i in range(0, len(sortedList)-1): if (sortedList[i] == sortedList[i+1]): return False return True
print("\n\n") print("Insertion sort: \n") temp = InsertionSort.insertionSort(tabl) print(temp) print("\n\n") print("Bubble sort: \n") temp = BubbleSort.bubbleSort(tabl) print(temp) print("\n\n") print("Quick sort: \n") temp = QuickSort.quickSort(tabl) print(temp) print("\n\n") print("Merge sort: \n") temp = MergeSort.mergeSort(tabl) print(temp) print("\n\n") print("Heap sort: \n") temp = HeapSort.heapSort(tabl) print(temp) print("\n\n") print("Bucket sort: \n") temp = BucketSort.bucketSort(tabl) print(temp) print("\n\n")