• 排序算法Nb三人组-归并排序


    归并排序只能对两个已经有序的列表进行合并排序,所以要我们自己创建出两个有序列表。最后在进行合并.

    def merge2list(li1, li2):
        li = []
        i = 0
        j = 0
        while i < len(li1) and j < len(li2):
            if li1[i] <= li2[j]:
                li.append(li1[i])
                i += 1
            else:
                li.append(li2[j])
                j += 1
        while i < len(li1):
            li.append(li1[i])
            i += 1
        while j < len(li2):
            li.append(li2[j])
            j += 1
        return li
    def merge(li, low, mid, high):
        # 列表两段有序: [low, mid] [mid+1, high]
        i = low
        j = mid + 1
        li_tmp = []
        while i <= mid and j <= high:
            if li[i] <= li[j]:
                li_tmp.append(li[i])
                i += 1
            else:
                li_tmp.append(li[j])
                j += 1
        while i <= mid:
            li_tmp.append(li[i])
            i += 1
        while j <= high:
            li_tmp.append(li[j])
            j += 1
        # li_tmp[0:high-low+1] li[low:high+1]
        for i in range(low, high+1):
            li[i] = li_tmp[i-low]
    def _merge_sort(li, low, high): #排序li的low到high的范围
        if low < high:
            mid = (low + high) // 2
            _merge_sort(li, low, mid)
            _merge_sort(li, mid+1, high)
            # print(li[low:mid + 1], li[mid + 1:high + 1])
            merge(li, low, mid, high)
            # print(li[low: high + 1])
  • 相关阅读:
    Silverlight 之 断点调试
    Silverlight 之 浅析
    Silverlight 之 新建项目解析
    Silverlight 之 创建
    有关TCP和UDP 粘包 消息保护边界
    计算机网络杂项
    RTP
    如何取消Linux下,vi中显示的^M符号
    Linux下实现定时器Timer的几种方法
    UNIX网络编程——套接字选项
  • 原文地址:https://www.cnblogs.com/Treasuremy/p/10432713.html
Copyright © 2020-2023  润新知