Created
March 20, 2014 12:03
-
-
Save mailpraveens/9662259 to your computer and use it in GitHub Desktop.
Heapify method which can be used in heap sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#max heap construction | |
def heapify(items,idx): | |
left = leftChild(idx) | |
right = rightChild(idx) | |
largest = -1 | |
if(left < len(items) and items[left] > items[idx]): | |
largest = left | |
else: | |
largest = idx | |
if(right < len(items) and items[right] > items[largest]): | |
largest = right | |
if(largest != idx): | |
temp = items[idx] | |
items[idx] = items[largest] | |
items[largest] = temp | |
heapify(items,largest) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment