zoukankan      html  css  js  c++  java
  • 归并排序 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的 归并排序确实很快

  • 相关阅读:
    css3-13 如何改变文本框的轮廓颜色
    css3-13 css3的3D动画如何实现
    poj 2565 Ants (KM+思维)
    C语言功能 --C
    jQuery简要dom操作
    最近ubuntu 14.04 cpu高入住故障排除
    spring framework 4 源代码阅读器(1) --- 事前准备
    基于Haar特征Adaboost人脸检测级联分类
    ZOJ-3652-Maze(BFS)
    设计模式
  • 原文地址:https://www.cnblogs.com/jackson1/p/12722820.html
Copyright © 2011-2022 走看看