Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #HeapSortAscendente
  2.  
  3. import math
  4.  
  5. def hIzq(i):
  6. return 2*i
  7.  
  8. def hDer(i):
  9. return 2*i+1
  10.  
  11. def intercambia (A, x, y):
  12. A[x], A[y] = A[y], A[x]
  13.  
  14. def MaxHeapify (A, i, tamanoHeap):
  15. L=hIzq(i)
  16. R=hDer(i)
  17. if (L <= tamanoHeap and A[L]>A[i]):
  18. posMax=L
  19. else:
  20. posMax=i
  21. if (R<=tamanoHeap and A[R]>A[posMax]):
  22. posMax=R
  23. if (posMax!=i):
  24. intercambia(A,i,posMax)
  25. Maxheapify(A,posMax,tamanoHeap)
  26.  
  27. def construirHeapMaxTni(A,tamanoHeap):
  28. for i in range(math.ceil(tamanoHeap/2)-1,0,-1):
  29. Maxheapify(A,i,tamanoHeap)
  30.  
  31. def OrdenacionHeapSort(A,tamanoHeap):
  32. construirHeapMaxTni(A,tamanoHeap)
  33. for i in range (leng(A[1:]),1,-1):
  34. intercambia(A,1,i)
  35. tamanoHeap=tamanoHeap-1
  36. MaxHeapify(A,1,tamanoHeap)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement