zoukankan      html  css  js  c++  java
  • 最大值堆排序算法

    def MaxHeap(heap,heapsize,i):#获取最大值堆核心算法
        left=2*i+1
        right=2*i+2
        sign=i
        if left:
            sign=left
        if right:
            sign=right
        if i!=sign:
            heap[i],heap[sign]=heap[sign],heap[i]
            MaxHeap(heap,heapsize,sign)
     
    def BuildHeap(heap):#获取最大值堆
        heapsize=len(heap)-1
        for i in xrange((heapsize//2),-1,-1):
            MaxHeap(heap,heapsize,i)
     
    def HeapSort(heap):#最大值堆排序
        BuildHeap(heap)
        print heap
        for i in xrange(len(heap)-1,-1,-1):
            heap[0],heap[i]=heap[i],heap[0]
            MaxHeap(heap,i,0)
        return heap
     
    if __name__=="__main__":
        heap=[100,1,100,1,100,1,100,1,100,1,2,3,2,1,2,1,4,3,2,2,4,21,1,100,1,2,1,223,13,212,11,11]
        print HeapSort(heap)
  • 相关阅读:
    ubuntu 16.04 安装显卡驱动,再安装cuda
    8. golang 基本类型转换
    7.golang的字符串 string
    5. 变量定义
    4. 代码规范
    3.golang 的注释
    1.windows server 201x
    exec 命令
    powershell
    1.Dockerfile
  • 原文地址:https://www.cnblogs.com/zhangtebie/p/11185842.html
Copyright © 2011-2022 走看看