博客
关于我
分而治之——归并
阅读量:436 次
发布时间:2019-03-06

本文共 669 字,大约阅读时间需要 2 分钟。

# 分而治之
def merge_sort(lists):
    if len(lists) <= 1:
        return lists
    else:
        mid = int(len(lists) / 2)
        left_half = merge_sort(lists[:mid])
        right_half = merge_sort(lists[mid:])
        new_lists = merge_sorted_list(left_half,right_half)
        return new_lists
def merge_sorted_list(sorted_a,sorted_b):
    length_a,length_b = len(sorted_a),len(sorted_b)
    a = b = 0
    new_sorted_list = list()
    while a < length_a and b < length_b:
        if sorted_a[a] < sorted_b[b]:
            new_sorted_list.append(sorted_a[a])
            a += 1
        else:
            new_sorted_list.append(sorted_b[b])
            b += 1
        
    while a < length_a:
        new_sorted_list.append(sorted_a[a])
        a += 1
    while b < length_b:
        new_sorted_list.append(sorted_b[b])
        b += 1
    
    return new_sorted_list

转载地址:http://iheyz.baihongyu.com/

你可能感兴趣的文章
微软面试题
查看>>
Google新玩法(转载)
查看>>
C#中Dispose和Close的区别!
查看>>
如何让服务在流量暴增的情况下保持稳定输出
查看>>
一个20年技术老兵的 2020 年度技术总结
查看>>
一例完整的websocket实现群聊demo
查看>>
【Net】ABP框架学习之它并不那么好用
查看>>
Git 笔记
查看>>
Harbor 批量清理历史镜像
查看>>
使用Azure Functions玩转Serverless
查看>>
.NET Core 基于Websocket的在线聊天室
查看>>
使用MySQL Shell创建MGR
查看>>
win10新版wsl2使用指南
查看>>
spring-boot 使用hibernate validation对参数进行优雅的校验
查看>>
关于我
查看>>
数据结构实验之栈四:后缀式求值
查看>>
图结构练习——最小生成树(prim算法(普里姆))
查看>>
sdut 2498【aoe 网上的关键路径】
查看>>
【PHP自定义显示系统级别的致命错误和用户级别的错误】
查看>>
【JAVA多线程中使用的方法】
查看>>