介绍
归并排序是采用分治法的一个非常典型的应用。归并排序的思想就是先递归分解数组,再合并数组。
先考虑合并两个有序数组,基本思路是比较两个数组的最前面的数,谁小就先取谁,取了后相应的指针就往后移一位。然后再比较,直至一个数组为空,最后把另一个数组的剩余部分复制过来即可。
再考虑递归分解,基本思路是将数组分解成left和right,如果这两个数组内部数据是有序的,那么就可以用上面合并数组的方法将这两个数组合并排序。如何让这两个数组内部是有序的?可以再二分,直至分解出的小组只含有一个元素时为止,此时认为该小组内部已有序。然后合并排序相邻二个小组即可。
代码
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 27 11:51:09 2016
@author: zang
"""
from matplotlib import pyplot as plt
import random
def mergeSort(ary):
if len(ary) <= 1 : return ary
num = int(len(ary)/2) #二分分解
left = mergeSort(ary[:num])
right = mergeSort(ary[num:])
return merge(left,right) #合并数组
def merge(left,right):
'''合并操作,
将两个有序数组left[]和right[]合并成一个大的有序数组'''
l,r = 0,0 #left与right数组的下标指针
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
def plotScatter(inputList):
plt.scatter(range(len(inputList)),inputList)
plt.show()
if __name__ == "__main__":
num_list = range(1000)
unsortedList = random.sample(num_list, 50)
print "unsortedList:"
plotScatter(unsortedList)
print unsortedList
sortedList = mergeSort(unsortedList)
print "sortedList:"
plotScatter(sortedList)
print sortedList
测试
输入
[140, 415, 660, 535, 165, 968, 416, 214, 994, 227, 293, 937, 637, 735, 590, 709, 938, 8, 840, 446, 566, 26, 858, 846, 710, 424, 865, 529, 980, 550]
输出
[8, 26, 140, 165, 214, 227, 293, 415, 416, 424, 446, 529, 535, 550, 566, 590, 637, 660, 709, 710, 735, 840, 846, 858, 865, 937, 938, 968, 980, 994]
分析
情况 | 性能 |
---|---|
Worst case performance: | |
Best case performance: | |
Average case performance: | |
Worst case space complexity: | - |