码迷,mamicode.com
首页 > 编程语言 > 详细

python实现分治法排序

时间:2014-05-14 21:59:28      阅读:440      评论:0      收藏:0      [点我收藏+]

标签: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))

python实现分治法排序,布布扣,bubuko.com

python实现分治法排序

标签:python 分治法排序 插入排序

原文地址:http://blog.csdn.net/startupmount/article/details/25811109

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!