示例#1
0
def heapsort(heap):
    """
    Algoritmo Heapsort implementado.
    """
    #construccion maxHeap
    size=hp.size(heap)
    mid=int(hp.size(heap))//2
    cmpfunction=heap['cmpfunction']
    i=mid
    while i in range(1,mid+1):
        lst=heap['elements']
        rs=i*2
        ls=i*2+1
        root=lt.getElement(lst,i)
        Right_son=lt.getElement(lst, rs)
        Left_son=lt.getElement(lst, ls)
        #necesario hacer recursiva esta parte
        if cmpfunction(root, Right_son):
            hp.sink(heap, i)
        elif cmpfunction(root, Left_son):
            hp.sink(heap, i)
        i-=1
    #invirtiendo pos
    j=size-1
    while j in range(1,size):
        hp.exchange(heap, 1, j)
        hp.sink(heap, 1)
        j-=1
示例#2
0
def size(minpq):
    """
    Retorna el número de elementos en la MinPQ
    Args:
        minpq: la cola de prioridad
    Returns:
       El tamaño de la MinPQ
    Raises:
        Exception
    """
    try:
        return (h.size(minpq['heap']))
    except Exception as exp:
        error.reraise(exp, 'minpq:size')