0

I have been trying to debug this code for hours. I don't know why it is not rearranging the terms. I have tried everything I can think of. Can someone help? Thanks.

public void heapify(int i)  // utility routine to percolate down from index i
{
    printHeap();
    int left, r, min;
    Process tmp;

    left = lchild(i);           // left child
    r = rchild(i);                  // right child

    if(left < size() && A[left].compareTo(A[i])<0)      // find smallest child
        min = left;                 // save index of smaller child
    else
        min = i;

    if(r < size() && A[r].compareTo(A[min])<0)
        min = r;                // save index of smaller child

    if(min != i)                // swap and percolate, if necessary
    {
        tmp = A[i];             // exchange values at two indices
        A[i] = A[min];
        A[min] = tmp;
        heapify(min);

        // call heapify
    }// end if
printHeap();
}// end method heapify


private int lchild(int i) {
    return 2 * i + 1;
}

private int rchild(int i) {
    return 2 * i + 2;
}

Even when I call heapify on every element of the heap it doesn't work :/ Here is the compareTo. It is supposed to arrange max heap using priority first then if there is a tie it goes to a unique time arrived value.

public int compareTo(Process o) {
    int val;
    if (this.priority > o.getPriority()) {
        val = -1;
    } else if (this.priority == o.getPriority()) {
        if (this.arrivalTime < o.getArrivalTime()) { //Earlier time
            val = -1;
        } else {
            val = 1;
        }
    } else {
        val = 1;
    }

    return val;
}
Math4Life
  • 125
  • 7
  • It works when I try to run it. What is your test example where it doesn’t? – Ole V.V. Sep 15 '16 at 05:24
  • What exactly are you trying to do here? Are you trying to rearrange the items in an array (`A` in your example) so that they form a valid binary heap? – Jim Mischel Sep 15 '16 at 08:02
  • The SO would have to know; I took this comment to tell the story: “utility routine to percolate down from index i”, could be mistaken. – Ole V.V. Sep 15 '16 at 09:15
  • I was hoping that what it would do is organize the max heap if I run it for every element. Is this correct? If not how would I organize my entire max heap? Thanks. – Math4Life Sep 15 '16 at 13:32
  • I believe for your method to organize a heap correctly you will have to call it on all elements in the first half of the array *backwards*: `for (int index = A.length / 2; index >= 0; index--) { heapify(index); }`. Not sure about the exact place to start (+/- 1), and with length 0 the code I just gave will throw an ArrayIndexOutOfBoundsException, so at least adjust to that. – Ole V.V. Sep 16 '16 at 07:55

2 Answers2

1

The fastest known way to organize an array into a heap is called Floyd's Algorithm. You start at the middle of the array and move towards the root, sifting each item down as required. In your case:

for (int i = size()/2; i >= 0; --i)
{
    heapify(i);
}

You should be able to call the heapify function that you supplied.

To see how this works, take a look at https://stackoverflow.com/a/39020777/56778

Community
  • 1
  • 1
Jim Mischel
  • 122,159
  • 16
  • 161
  • 305
  • Thank you. I appreciate you looking at it. It still doesn't seem to be sorting properly but I will keep looking at it. – Math4Life Sep 15 '16 at 15:47
  • Thank you. I appreciate you looking at it. It still doesn't seem to be sorting properly but I will keep looking at it. Maybe it is. I could just be mis reading the debugger. – Math4Life Sep 15 '16 at 15:53
  • 1
    @ryBear Perhaps you should edit your question to show the actual code you're using, the input array, the expected output, and the actual output. That way we would have enough information to diagnose the problem. – Jim Mischel Sep 15 '16 at 18:22
0

I figured it out. It wasn't a problem in this method after all. Thanks everyone!

Math4Life
  • 125
  • 7