堆排序

作者: wncbbnk | 来源:发表于2018-10-21 15:32 被阅读0次
    PARENT(i)
    --return (i-1)/2
    
    LEFT(i)
    --return i*2+1
    
    RIGHT(i)
    --return i*2+2
    
    MAX_HEAPIFY(A, i)
    --l=LEFT(i)
    --r=RIGHT(i)
    --if l<=A.heapSize and A[l]>A[i]
    ----largest=l
    --else
    ----largest=i
    --if r<=A.heapSize and A[r]>A[largest]
    ----largest=r
    --if largest!=i
    ----exchange A[i] with A[largest]
    ----MAX_HEAPIFY(A, largest)
    
    BUILD_MAX_HEAP(A)
    --A.heapSize=A.length
    --for i =A.heapSize/2-1 down to 0
    ----MAX_HEAPIFY(A, i)
    
    HEAPSORT(A
    --BUILD_MAX_HEAP(A)
    --for i=A.length-1 down to 1
    ----exchange A[0] with A[i]
    ----A.heapSize=A.heapSize-1
    ----MAX_HEAPIFY(A, 0)
    
    

    相关文章

      网友评论

          本文标题:堆排序

          本文链接:https://www.haomeiwen.com/subject/rpubzftx.html