It is a heap sort.
def Parent(i):
    return i / 2
def Left(i):
    return 2 * i
def Right(i):
    return 2 * i + 1
def MAX_HEAPIFY(A, i):    # A.heapSize == A[0]
    l = Left(i)
    r = Right(i)
Here is the problem.
    if l <= A[0] and A[l] > A[i]:
If I write l <= A[0] and A[l] > A[i], then the code can run correctly.
But if I switch the two conditions, like A[l] > A[i] and l <= A[0], then there is a error. IndexError: list index out of range
        largest = l
    else: largest = i
    if r <= A[0] and A[r] > A[largest]:
        largest = r
    if largest != i:
        temp = A[i]
        A[i] = A[largest]
        A[largest] = temp
        print "exchange parent %d with child %d" %(A[largest], A[i]) 
        MAX_HEAPIFY(A, largest)
def BUILD_MAX_HEAP(A):
    A[0] = len(A) - 1
    for i in range(len(A) / 2, 0, -1):
        MAX_HEAPIFY(A, i)
def HEAPSORT(A):
    BUILD_MAX_HEAP(A)
    print A
    for i in range(len(A) - 1, 1, -1):
        temp = A[1]
        A[1] = A[i]
        A[i] = temp
        A[0] = A[0] - 1   # A.heapSize - 1
        print "exchange parent %d with child %d the last" %(A[i], A[1]) 
        MAX_HEAPIFY(A, 1)
Thank you for your help.
 
    