Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. Function BuildMaxHeap(Int arr, Int n, String theKey)
  2. Int i = 1
  3. While (i < n)
  4. If (jMap.getFlt(jArray.getObj(arr, i), theKey) > jMap.getFlt(jArray.getObj(arr, (i - 1) / 2), theKey))
  5. Int j = i
  6. While (jMap.getFlt(jArray.getObj(arr, j), theKey) > jMap.getFlt(jArray.getObj(arr, (j - 1) / 2), theKey))
  7. jArray.swapItems(arr, j, (j - 1) / 2)
  8. j = (j - 1) / 2
  9. EndWhile
  10. EndIf
  11. i += 1
  12. EndWhile
  13. EndFunction
  14.  
  15.  
  16. Function IterativeHeapSort(Int arr, Int n, String theKey)
  17. BuildMaxHeap(arr, n, theKey)
  18.  
  19. Int i = n - 1
  20. While (i > 0)
  21. jArray.swapItems(arr, 0, i)
  22.  
  23. Int j = 0
  24. Int index
  25.  
  26. Bool do = True ; Modified do-while
  27. While(do || index < 1)
  28. index = 2 * j + 1
  29.  
  30. If (jMap.getFlt(jArray.getObj(arr, index), theKey) < jMap.getFlt(jArray.getObj(arr, index + 1), theKey) && index < (i - 1))
  31. index += 1
  32. EndIf
  33.  
  34. If (jMap.getFlt(jArray.getObj(arr, j), theKey) < jMap.getFlt(jArray.getObj(arr, index), theKey) && index < i)
  35. jArray.swapItems(arr, j, index)
  36. EndIf
  37.  
  38. j = index
  39. do = False
  40. EndWhile
  41.  
  42. i -= 1
  43. EndWhile
  44. EndFunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement