Python编写算法导论基本算法

插入排序

#-------------------------------------------------------------------------------
# Name:        Insert_Order
#
# Author:      Administrator
#
# Created:     05/07/2015
# Copyright:   (c) Administrator 2015
# Licence:     
#-------------------------------------------------------------------------------
def Insert_Order(nums):
    nums_len = len(nums)
    for i in range(0,nums_len):
        key = nums[i]
        j = i-1
        while (j>=0) and (nums[j]>key):
            nums[j+1] = nums[j]
            j = j-1
        nums[j+1] = key
    for i in range(0,nums_len):
        print nums[i]


def main():
    Test_Nums = [5,2,4,6,1,3]
    Insert_Order(Test_Nums)
    pass

if __name__ == '__main__':

    main()


合并排序

#-------------------------------------------------------------------------------
# Name:        Merge_Sort
# Purpose:
#
# Author:      Administrator
#
# Created:     05/07/2015
# Copyright:   (c) Administrator 2015
# Licence:     
#-------------------------------------------------------------------------------

def show_nums(nums):
    for i in range(len(nums)):
        print nums[i]

def swap(a,b):
    key = a
    a = b
    b = key
    return a,b

def Merge(nums,p,q,r):
    nums2 = nums[:]
    i = p
    j = q + 1
    for count in range(p,r+1):
        if(nums[i]>nums[j]):
            nums2[count] = nums[j]
            if(j+1>r):
                j = r
                nums[j] = 1000  #Infinite
            else:
                j = j + 1
        elif(nums[i]q):
                i = q
                nums[i] = 1000  #Infinite
            else:
                i = i + 1
    return nums2

def Merge_Sort(nums,p,r):
    if(p==r-1):
        if(nums[p]>nums[r]):
            nums[p],nums[r] = swap(nums[p],nums[r])
    elif(p==r):
        pass
    else:
        q = p + (r-p)/2
        nums = Merge_Sort(nums,p,q)
        nums = Merge_Sort(nums,q+1,r)
        nums = Merge(nums,p,q,r)
    return nums

def main():
    nums_Test = [4,2,5,1,7,6,3,0]
    nums_Test = Merge_Sort(nums_Test,0,7)
    show_nums(nums_Test)
    pass

if __name__ == '__main__':
    main()


最大树排序

#-------------------------------------------------------------------------------
# Name:        HeapSort
# Purpose:
#
# Author:      Administrator
#
# Created:     06/07/2015
# Copyright:   (c) Administrator 2015
# Licence:     
#-------------------------------------------------------------------------------
import BTree

def HeapSort(btree,nums):
    for i in range(btree.count):
        nums.append(btree.root.data)
        if(btree.root.left.data>btree.root.right.data):
            btree.root.data = btree.root.left.data
            btree.root.left.data = -1
        else:
            btree.root.data = btree.root.right.data
            btree.root.right.data = -1
        btree.MaxHeap(btree.root)
    for j in range(len(nums)):
        print nums[j]

def main():
    n10 = BTree.TreeNode(10,0,0)
    n9  = BTree.TreeNode(9,0,0)
    n3  = BTree.TreeNode(3,n9,n10)
    n8  = BTree.TreeNode(8,0,0)
    n14 = BTree.TreeNode(14,0,0)
    n7  = BTree.TreeNode(7,0,0)
    n16 = BTree.TreeNode(16,n7,0)
    n2  = BTree.TreeNode(2,n14,n8)
    n1  = BTree.TreeNode(1,n2,n16)
    root = BTree.TreeNode(4,n1,n3)

    bt = BTree.BTree(root=root)
    bt.BuildMaxTree(bt.root)
    #bt.preOrder(bt.root)
    bt.nodeCount(bt.root)

    nums = []
    HeapSort(bt,nums)

    pass

if __name__ == '__main__':
    main()

快速排序

#-------------------------------------------------------------------------------
# Name:        QuickSort
# Purpose:
#
# Author:      Administrator
#
# Created:     06/07/2015
# Copyright:   (c) Administrator 2015
# Licence:     
#-------------------------------------------------------------------------------

def swap(a,b):
    key = a
    a = b
    b = key
    return a,b

def partition(nums,begin,end):
    flag = nums[end]
    mid = begin
    for i in range(begin,end+1):
        if(nums[i]

计数排序

#-------------------------------------------------------------------------------
# Name:        CountingSort
# Purpose:
#
# Author:      Administrator
#
# Created:     06/07/2015
# Copyright:   (c) Administrator 2015
# Licence:     
#-------------------------------------------------------------------------------

def CountingSort(nums,k):
    result = []
    for i in range(k+1):
        result.append(0)
    for i in range(len(nums)):
        result[nums[i]] = result[nums[i]] + 1
    count = 0
    for i in range(len(result)):
        for j in range(result[i]):
            nums[count] = i
            count = count + 1
    return nums

def main():
    nums = [2,5,3,0,2,3,0,3]
    nums = CountingSort(nums,5)
    print nums
    pass

if __name__ == '__main__':
    main()

基数排序

#-------------------------------------------------------------------------------
# Name:        RadixSort
# Purpose:
#
# Author:      Administrator
#
# Created:     07/07/2015
# Copyright:   (c) Administrator 2015
# Licence:     
#-------------------------------------------------------------------------------

import timeit
import re

def swap(a,b):
    key = a
    a = b
    b = key
    return a,b

def getNumber(num,k):#获取倒数第k个数字
    p = re.search('\d{%d}$'%k,str(num))
    r = re.search('^\d',p.group(0))
    return int(r.group(0))

def RadixSort(nums,k):#k代表为三位数组合
    for i in range(1,k+1):#内部为冒泡排序方法
        j = len(nums)
        while(not(j<0)):
            for k in range(j-1):
                if(getNumber(nums[k],i)>getNumber(nums[k+1],i)):
                    nums[k],nums[k+1] = swap(nums[k],nums[k+1])
            j -= 1
    print nums
    #return nums

if __name__ == '__main__':
    Nums = [329,457,657,839,436,720,355]

    tt = timeit.Timer('RadixSort(Nums,3)','from __main__ import RadixSort')
    print tt.timeit(0)






你可能感兴趣的:(Python学习)