Skip to content

Instantly share code, notes, and snippets.

@bcambel
Created November 6, 2014 09:23
Show Gist options
  • Select an option

  • Save bcambel/542155ddfbb564ed6440 to your computer and use it in GitHub Desktop.

Select an option

Save bcambel/542155ddfbb564ed6440 to your computer and use it in GitHub Desktop.
Heap sort implementation
def swap(i, j):
sqc[i], sqc[j] = sqc[j], sqc[i]
def heapify(end,i):
l=2 * i + 1
r=2 * (i + 1)
max=i
if l < end and sqc[i] < sqc[l]:
max = l
if r < end and sqc[max] < sqc[r]:
max = r
if max != i:
swap(i, max)
heapify(end, max)
def heap_sort():
end = len(sqc)
start = end / 2 - 1
for i in range(start, -1, -1):
heapify(end, i)
for i in range(end-1, 0, -1):
swap(i, 0)
heapify(i, 0)
sqc = [2, 7, 1, -2, 56, 5, 3]
heap_sort()
print(sqc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment