Advertisement
joharido

Untitled

Mar 13th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. 2. Algorithm heapify(A)
  2. 3. For i ← ⌊size of array A[ ]/2⌋-1 down to and including 0 do
  3. 4. maxHeapify(A, i)
  4. 5. End for
  5. 6. End
  6. 7.
  7. 8. Algorithm maxHeapify(A, n)
  8. 9. L ← 2i + 1
  9. 10. R ← 2i + 2
  10. 11. largest
  11. 12. If (hasLeft(A, n) and A[L] > A[n]) then
  12. 13. largest ← L
  13. 14. End if
  14. 15. Else
  15. 16. Largest ← n
  16. 17. End else
  17. 18. If (hasRight(A, n) and A[r] > A[largest])
  18. 19. Largest ← r
  19. 20. End if
  20. 21. If (largest ≠ n) then
  21. 22. Exchange A[n] with A[largest]
  22. 23. maxHeapify(A, largest)
  23. 24. End if
  24. 25. End
  25. 26.
  26. 27. public boolean hasLeft(A, n){
  27. 28. return 2i + 1 ≤ length of A
  28. 29. End if
  29. 30.
  30. 31. public Boolean hasRight(A, n)
  31. 32. return 2i + 2 ≤ length of A
  32. 33. End
  33. 34. D
  34. 35. Algorithm topFlyers(A)
  35. 36. Array result with the same size as the given array of passengers
  36. 37. for i ← 0 up to log n do
  37. 38. result[i] ← A[i]
  38. 39. End for
  39. 40. Return result
  40. 41. End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement