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

[算法导论]merge sort @ Python

时间:2014-08-17 15:30:52      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   for   div   log   算法   ef   

import sys

class mergesort():

    def merge_sort(self, A, p, r):
        if p < r:
            q = (p + r) / 2
            self.merge_sort(A, p, q)
            self.merge_sort(A, q+1, r)
            self.merge(A, p, q, r)
            return A

    def merge(self, A, p, q, r):
        n1 = q - p + 1
        n2 = r - q

        L = [0 for i in range(n1+1)]
        R = [0 for i in range(n2+1)]

        for i in range(n1):
            L[i] = A[p+i]
        for j in range(n2):
            R[j] = A[q+j+1]

        L[n1] = sys.maxint
        R[n2] = sys.maxint

        i = 0; j = 0
        for k in range(p, r):
            if L[i] <= R[j]:
                A[k] = L[i]
                i += 1
            else:
                A[k] = R[j]
                j += 1

sort = mergesort()

A = [1,3,5,7,9,2,4,6,8,10]

print sort.merge_sort(A, 0, len(A)-1)

 

[算法导论]merge sort @ Python,布布扣,bubuko.com

[算法导论]merge sort @ Python

标签:style   blog   color   for   div   log   算法   ef   

原文地址:http://www.cnblogs.com/zuoyuan/p/3917800.html

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