首页 > 代码库 > python实现分治法排序
python实现分治法排序
# -*- coding: utf-8 -*- """ Created on Wed May 14 16:14:50 2014 @author: lifeix """ def merge(a, start, mid, end): if start == end: return a if mid == 0: return a temp1 = a[start:mid] m1 = len(temp1)/2 print temp1,‘----‘,m1 newtemp1 = merge(temp1,0,m1,len(temp1)) temp2 = a[mid:end] m2 = len(temp2)/2 newtemp2 = merge(temp2,0,m2,len(temp2)) #通过插入排序合并被排序的两个子数组 for i in range(len(newtemp1)): t = newtemp1[i] for j in range(len(newtemp2)): if t > newtemp2[j]: temp = t t = newtemp2[j] newtemp2[j] = temp newtemp1[i] = t return newtemp1+newtemp2 a = [1,5,3,78,4,56,21] mid = len(a)/2 merge(a,0,mid, len(a))
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。