Python实现各类数据结构和算法

数据结构和算法——python实现

#
-------------------------------------目录START--------------------------------
模块一:经典排序实现
交换类排序法:归并排序|冒泡排序|快速排序
选择类排序法:直接选择排序|堆排序
插入类排序法:插入排序|希尔排序|折半查找插入排序
分配排序:桶排序|基数排序
非比较类排序:计数排序
模块二:经典查找
静态查找:折半查找|顺序查找|分块查找
动态查找:二叉排序树|平衡二叉树
模块三:数据结构
线性表、栈和队列、串和数组、树和二叉树、图:
图的遍历
深度优先算法|广度优先算法
生成树和最小生成树:Prim算法|Kruskal算法
最短路径:Dijkstra算法|floy算法
拓扑排序:无前驱的顶点优先的拓扑排序方法|无后继的顶点优先拓扑排序方法
模块四
高级设计和分析技术
动态规划:最长公共子序列|钢条切割|背包问题
贪心算法:活动选择问题
摊还分析:二进制计数器递增

#==============================
排序算法:
import copy
nums = [4,2,5,3,1,8,20,31,24,35,6]  
bubble_nums = copy.deepcopy(nums)
insert_nums   = copy.deepcopy(nums)
quick_nums    = copy.deepcopy(nums)
select_nums   = copy.deepcopy(nums)
heap_nums    = copy.deepcopy(nums)
merge_nums  = copy.deepcopy(nums)
#=====================================================================
def bubble_sort(nums):
    for i in xrange(len(nums)):
        for j in xrange(i+1,len(nums)):
            if nums[i] > nums[j]:#前面比后面大,那么置换
                nums[i],nums[j] = nums[j],nums[i]
    return nums
print bubble_nums
print bubble_sort(bubble_nums)
#=====================================================================
def insert_sort(nums):
    if not nums:
        return []
    res = []
    res += nums[0],
    for i in xrange(1,len(nums)):
        res += nums[i],#先将元素放在最后面,然后向前比较
        for j in xrange(len(res)-1, 0, -1):
            if res[j-1] > res[j]:#前面的大,置换
                res[j], res[j-1] = res[j-1], res[j]
    return res
print insert_nums
print insert_sort(insert_nums)
#=====================================================================
# def partition(nums, left, right):
#     x = nums[right]#最后一个元素值
#     i = left - 1
#     for j in range(left, right-1):
#         if nums[j] <= x:
#             i+=1
#             nums[i],nums[j] = nums[j], nums[i]
#     nums[i+1], nums[right] = nums[right],nums[i+1]
#     return i+1
def quick_sort(nums, left, right):#思想参考算法导论P96页的图
    if left < right:
        #q = partition(nums, left, right)
        x = nums[right]#最后一个元素值
        i = left - 1
        for j in range(left, right-1):
            if nums[j] <= x:
                i+=1
                nums[i],nums[j] = nums[j], nums[i]
        nums[i+1], nums[right] = nums[right],nums[i+1]

        quick_sort(nums, left, i)
        quick_sort(nums, i+2, right)#注意i+1,不是i,已经排序的那个不用排序了
    return nums
print quick_nums
print quick_sort(quick_nums, 0, len(quick_nums)-1)
#=====================================================================
def select_sort(nums):
    for i in range(0, len(nums)):
        m = i#记录当前元素
        for j in range(i+1, len(nums)):#剩下元素
            if nums[i] > nums[j]:
                m = j#记录剩下元素中最小的元素下标
        nums[m], nums[i] = nums[i], nums[m]
    return nums
print select_nums
print select_sort(select_nums)
#=====================================================================
def adjust_heap(nums, i, size):
    lchild = 2*i + 1#左右子树根节点的下标
    rchild = 2*i + 2
    m = i
    if i nums[m]:
            m = lchild
        if rchild < size and nums[rchild] > nums[m]:
            m = rchild
        if m != i:
            nums[m], nums[i] = nums[i], nums[m]
            adjust_heap(nums, m, size)

def build_heap(nums):
    for i in range(0, len(nums)/2)[::-1]:#叶子节点
        adjust_heap(nums, i, len(nums))
def heap_sort(nums):
    build_heap(nums)
    for i in range(0, len(nums))[::-1]:
        nums[0], nums[i] = nums[i], nums[0]
        adjust_heap(nums, 0, i)
    return nums
print heap_nums
print heap_sort(heap_nums)
#=====================================================================
def merge(left, right):
    i, j = 0, 0
    result = []
    while i < len(left) and j < len(right):#将两个有序数组合并到一起
        if left[i] <= right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
    result += left[i:]#追加后续没有进行比较的元素
    result += right[j:]
    return result

def merge_sort(nums):
    if len(nums)<=1:
        return nums
    num = len(nums)/2
    left  = merge_sort(nums[:num])
    right = merge_sort(nums[num:])
    return merge(left, right)#把左右区间用一次归并操作合并成有序的区间

print merge_nums
print merge_sort(merge_nums)
#=====================================================================

--------------------------------------目录END--------------------------------

参考网站:

[1].http://www.cnblogs.com/coder2012/archive/2013/05/19/3079140.html

[3].http://baike.baidu.com/view/1906331.htm

[2]http://blog.csdn.net/wklken/article/details/7554989

----------------------------------------------------------------------

你可能感兴趣的:(python,函数)