毛老師算法分析課作業合並排序法
阿新 • • 發佈:2018-09-18
dom main return sta con list() === start RoCE
實現合並排序:
# -*- coding: utf-8 -*- # @Time : 18-9-18 上午10:59 # @Author : Guo Zhengbing # @Email : [email protected] #產生一個1000個元素的數組 import random def random_int_list(start, stop, length): start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start)) length = int(abs(length)) iflength else 0 random_list = [] for i in range(length): random_list.append(random.randint(start, stop)) return random_list a = random_int_list(1,1000,20) #合並排序 def MergeSort(lists): if len(lists) <= 1: return lists mid = len(lists)//2 #遞歸 listA = MergeSort(lists[:mid]) listB= MergeSort(lists[mid:]) print("========listA========") print(listA) print("========listB========") print(listB) return MergeSortedLists(listA, listB) #合並兩個有序數集 def MergeSortedLists(listA, listB): newList = list() a = 0 b = 0 # Merge the two lists together until one is emptywhile a < len(listA) and b < len(listB): if listA[a] < listB[b]: newList.append(listA[a]) a += 1 else: newList.append(listB[b]) b += 1 # If listA contains more items,append them to newList while a < len(listA): newList.append(listA[a]) a += 1 while b < len(listB): newList.append(listB[b]) b += 1 return newList # If listB contains more items,append them to newList if __name__ == "__main__": lists = a print(lists) result = MergeSort(lists) print("排序前:", a) print("排序後:", result)
輸出:
[748, 256, 946, 364, 496, 807, 758, 689, 274, 767, 962, 534, 434, 481, 464, 538, 652, 547, 483, 472] ========listA======== [748] ========listB======== [256] ========listA======== [364] ========listB======== [496] ========listA======== [946] ========listB======== [364, 496] ========listA======== [256, 748] ========listB======== [364, 496, 946] ========listA======== [807] ========listB======== [758] ========listA======== [274] ========listB======== [767] ========listA======== [689] ========listB======== [274, 767] ========listA======== [758, 807] ========listB======== [274, 689, 767] ========listA======== [256, 364, 496, 748, 946] ========listB======== [274, 689, 758, 767, 807] ========listA======== [962] ========listB======== [534] ========listA======== [481] ========listB======== [464] ========listA======== [434] ========listB======== [464, 481] ========listA======== [534, 962] ========listB======== [434, 464, 481] ========listA======== [538] ========listB======== [652] ========listA======== [483] ========listB======== [472] ========listA======== [547] ========listB======== [472, 483] ========listA======== [538, 652] ========listB======== [472, 483, 547] ========listA======== [434, 464, 481, 534, 962] ========listB======== [472, 483, 538, 547, 652] ========listA======== [256, 274, 364, 496, 689, 748, 758, 767, 807, 946] ========listB======== [434, 464, 472, 481, 483, 534, 538, 547, 652, 962] 排序前: [748, 256, 946, 364, 496, 807, 758, 689, 274, 767, 962, 534, 434, 481, 464, 538, 652, 547, 483, 472] 排序後: [256, 274, 364, 434, 464, 472, 481, 483, 496, 534, 538, 547, 652, 689, 748, 758, 767, 807, 946, 962] Process finished with exit code 0
毛老師算法分析課作業合並排序法