归并排序 VS 选择排序
import time, random
def random_List(start, end, length):
random_array = []
for i in range(0, length):
random_array.append(random.randint(start, end))
return random_array
def merge_sort(arrayList):
n = len(arrayList)
if n <= 1:
return arrayList
mid = n // 2
left_array = merge_sort(arrayList[:mid])
right_array = merge_sort(arrayList[mid:])
left, right = 0, 0
newList = []
while left < len(left_array) and right < len(right_array):
if left_array[left] < right_array[right]:
newList.append(left_array[left])
left += 1
else:
newList.append(right_array[right])
right += 1
newList += left_array[left:]
newList += right_array[right:]
return newList
def select_sort(arrayList):
for i in range(0, len(arrayList)):
min = i
for j in range(i, len(arrayList) - 1):
if arrayList[j] < arrayList[min]:
min = j
arrayList[min], arrayList[i] = arrayList[i], arrayList[min]
return arrayList
if __name__ == "__main__":
array = random_List(1, 100, 10000)
print(array)
start_time = time.time()
result = merge_sort(array)
ex_time = time.time() - start_time
print(ex_time)
print(result)
print('--------------------')
print(array)
start_time = time.time()
result = select_sort(array)
ex_time = time.time() - start_time
print(ex_time)
print(result)
数组长度为10000, 来看看执行 的结果吧
看来作为选择排序PLUS的 归并排序确实很快
感谢您花时间阅读此篇文章,如果您觉得看了这篇文章之后心情还比较高兴,可以打赏一下,请博主喝上一杯咖啡,让博主继续码字……
本文版权归作者和博客园共有,来源网址:https://blog.csdn.net/weixin_46498102 欢迎各位转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接