fork download
  1. def swap(i, j):
  2. sqc[i], sqc[j] = sqc[j], sqc[i]
  3.  
  4. def heapify(end,i):
  5. l=3 * (i + 1)
  6. m=3 * (i + 2)
  7. r=3 * (i + 3)
  8. max=i
  9. if l < end and sqc[i] < sqc[l]:
  10. max = l
  11. if r < end and sqc[max] < sqc[r]:
  12. max = r
  13. if m < end and sqc[max] < sqc[m]:
  14. max = m
  15. if max != i:
  16. swap(i, max)
  17. heapify(end, max)
  18.  
  19. def heap_sort():
  20. end = len(sqc)
  21. start = end / 2 - 1
  22. for i in range(start, -1, -1):
  23. heapify(end, i)
  24. for i in range(end-1, 0, -1):
  25. swap(i, 0)
  26. heapify(i, 0)
  27.  
  28. sqc = [2, 7, 1, -2, 56, 5, 3]
  29. heap_sort()
  30. print(sqc)
Success #stdin #stdout 0.07s 8832KB
stdin
Standard input is empty
stdout
[7, 1, -2, 56, 5, 2, 3]