Guest User

Untitled

a guest
Oct 18th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. public T remove() {
  2. if (size == 0) {
  3. throw new java.util.NoSuchElementException();
  4. }
  5. T result = array[1];
  6. array[1] = array[size];
  7. array[size] = null;
  8. size--;
  9. if (size > 0) {
  10. int i = 1;
  11. while ((i * 2) + 1 <= size) {
  12. if (array[i].compareTo(array[i * 2]) > 0
  13. && array[i * 2].compareTo(array[(i * 2) + 1]) < 0) {
  14. swap(array, i, i * 2);
  15. i = i * 2;
  16. } else if (array[i].compareTo(array[(i * 2) + 1]) > 0
  17. && array[(i * 2) + 1].compareTo(array[i * 2]) < 0) {
  18. swap(array, i, (i * 2) + 1);
  19. i = (i * 2) + 1;
  20. }
  21. }
  22. }
  23. return result;
  24. }
Add Comment
Please, Sign In to add comment