归并排序算法-python实现
2015-09-23 11:17
603 查看
#-*- coding: UTF-8 -*- import numpy as np def Merge(a, f, m, l): i = f j = m + 1 tmp = [] while i <= m and j <= l: if a[i] <= a[j]: tmp.append(a[i]) i += 1 else: tmp.append(a[j]) j += 1 while i <= m: tmp.append(a[i]) i += 1 while j<= l: tmp.append(a[j]) j+= 1 i = f for x in xrange(0, len(tmp)): a[i] = tmp[x] i += 1 def MergeSort(a, f, l): if f< l: m = (l + f) / 2 MergeSort(a, f, m) MergeSort(a, m+1, l) Merge(a, f, m, l) if __name__ == '__main__': a = np.random.randint(0, 10, size = 10) print "Before sorting..." print "---------------------------------------------------------------" print a print "---------------------------------------------------------------" MergeSort(a, 0, a.size-1) print "After sorting..." print "---------------------------------------------------------------" print a print "---------------------------------------------------------------"
相关文章推荐
- 堆排序算法-python实现
- 插入排序算法-python实现
- 冒泡排序算法-Python实现
- 选择排序算法-python实现
- 快速排序算法-python实现
- python字典按照值排序
- python 时间模块小结
- Python科学计算发行版—Anaconda
- Python json.dumps
- 解决 python用 xlsxwrite 向excel 中写入中文字符串变量 报错
- Python环境及robotframework安装
- python中str函数isdigit、isdecimal、isnumeric的区别
- Python 爬虫进阶必须的几步
- Python特殊语法:filter、map、reduce、lambda
- Python yield 使用浅析
- 【Python之旅】第三篇(二):Pickle序列化
- python模拟用户登录某某网
- python encode和decode函数说明
- the file opeartion in python(一)
- Python多线程编程之多线程加锁