Last active
July 24, 2019 15:01
-
-
Save maksadbek/afd7d798363ec6e77e592a54b56ec99c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def heapify(a, n, i): | |
largest = i | |
l = 2 * i + 1 | |
r = 2 * i + 2 | |
if l < n and a[i] < a[l]: | |
largest = l | |
if r < n and a[largest] < a[r]: | |
largest = r | |
if largest != i: | |
a[i], a[largest] = a[largest], a[i] | |
heapify(a, n, largest) | |
def sort(a): | |
n = len(a) | |
for i in range(n, -1, -1): | |
heapify(a, n, i) | |
for i in range(n-1, 0, -1): | |
a[i], a[0] = a[0], a[i] | |
heapify(a, i, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment