def fixheap(array, heaplen, index, greaterthan): |
def fixheap(array, heaplen, index): |
|
|
if (lc < heaplen and greaterthan(array[lc], array[index]) and (rc >= heaplen or (rc < heaplen and not greaterthan(array[rc], array[lc])))): (array[index], array[lc]) = (array[lc], array[index]) fixheap(array, heaplen, lc, greaterthan) |
if (lc < heaplen and array[lc] > array[index] and (rc >= heaplen or (rc < heaplen and array[rc] <= array[lc]))): array[index], array[lc] = array[lc], array[index] fixheap(array, heaplen, lc) |
elif (rc < heaplen and greaterthan(array[rc], array[index]) and greaterthan(array[rc], array[lc])): (array[index], array[rc]) = (array[rc], array[index]) fixheap(array, heaplen, rc, greaterthan) |
elif (rc < heaplen and array[rc] > array[index] and array[rc] > array[lc]): array[index], array[rc] = array[rc], array[index] fixheap(array, heaplen, rc) |
def makeheap(array, greaterthan): |
def makeheap(array): |
fixheap(array, len(array), i, greaterthan) |
fixheap(array, len(array), i) |
def heapsort(array, cmp=lambda x, y: x > y): makeheap(array, cmp) |
def heapsort(array): makeheap(array) |
fixheap(array, i, 0, cmp) |
fixheap(array, i, 0) |