标签:span 数列 turn 比较大小 return 大小 app def while
一、归并排序
先通过递归将列表元素分开,然后通过合并比较大小进行排序。
二、上代码
def merge(li, low, mid, high): """ 归并排序 :param li:参数列表 :param low: 列表最左边 :param mid: 列表中间的 :param high: 列表最右边 :return: """ i = low j = mid + 1 ltmp = [] while i <= mid and j <= high: if li[i] < li[j]: ltmp.append(li[i]) i += 1 else: ltmp.append(li[j]) j += 1 while i <= mid: ltmp.append(li[i]) i += 1 while j <= high: ltmp.append(li[j]) j += 1 li[low:high+1] = ltmp li = [2, 4, 5, 6, 1, 3, 7, 8, 9] # merge(li,0,3,8) # print(li) def merge_sort(li, low, high): if low < high: mid = (low + high) // 2 merge_sort(li, low, mid) merge_sort(li, mid+1, high) merge(li, low, mid, high) merge_sort(li, 0, 8) print(li)
标签:span 数列 turn 比较大小 return 大小 app def while
原文地址:https://www.cnblogs.com/youhongliang/p/12904728.html