#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/3/18 14:26 # @Author : baoshan # @Site : # @File : mergeSort.py # @Software: PyCharm Community Edition def MergeSort(lists): if len(lists) <= 1: return lists num = int(len(lists) / 2) left = MergeSort(lists[:num]) right = MergeSort(lists[num:]) return Merge(left, right) def Merge(left, right): l, r = 0, 0 result = [] while l < len(left) and r < len(right): if left[l] < right[r]: result.append(left[l]) l += 1 else: result.append(right[r]) r += 1 result += left[l:] result += right[r:] return result print(MergeSort([1, 2, 3, 4, 5, 6, 7, 90, 21, 23, 45]))
输出结果:
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/baoshan/PycharmProjects/myProject/python_weixin_study/mergeSort.py [1, 2, 3, 4, 5, 6, 7, 21, 23, 45, 90] Process finished with exit code 0