1

My code is working during compilation but the runtime error points to an if condition in my MaxHeapify function. I marked it out. Any help would be simply lovely, and would make me stop banging my head against the wall.

public class HeapSort {

    public static void main(String[] args) {
        int A[] = {-100, 1, 2, 5, 7, 2, 9};
        Heapsort(A);
        //System.out.println(A.length);
        for (int x = 1; x < A.length; x++) {
            System.out.println(A[x] + " ");
        }
    }

    public static void Build_MAX_heap(int A[]) {
        int n = A.length;
        for (int i = n / 2; i >= 1; i--) {
            MAX_heapify(A, i);
        }
    }

    public static void MAX_heapify(int A[], int i) {
        int heapsize = A.length;
        int l = 2 * i;
        int r = 2 * i + 1;
        //find the largest of A[i].A[l],A[r]
        int largest = i;
        if (A[l] > A[largest]) {
            largest = l;
            if (A[l] > A[largest] && l <= heapsize) {
                largest = l;
            }
            if (A[r] > A[largest] && r <= heapsize) {     //runtime error here
                largest = r;
            }
            if (largest != i) {
                int temp = A[i];
                A[i] = A[largest];
                A[largest] = temp;
                MAX_heapify(A, largest);
            }
        }
    }

    public static void Heapsort(int A[]) {

        Build_MAX_heap(A);
        int heapsize = A.length;
        for (int last = heapsize; last >= 2; last--) {   
            int temp = A[1];
            A[1] = A[last];
            A[last] = temp;
            heapsize--;
            MAX_heapify(A,1);
        }
    }
}
1
  • Well, what is the error?? Commented Oct 21, 2015 at 15:59

3 Answers 3

1

You're doing you bounds checking for r (and l, too, in the previous statement) after checking the element at index r. If r is out of bounds, the first half of the expression will throw an exception, and never make it to the bounds check.

Your if statements should be structured

if (r < heapsize && A[r] > A[largest]) ...

so that you know you're in bounds before you start trying to access your array. In addition, since arrays are zero-indexed, the index of heapsize isn't valid, so r needs to be less than, not less than or equal to.

Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is that your are checking A[r] before checking if r is out of range so i would try to modify the code this way

 public class HeapSort {

public static void main(String[] args) {
    int A[] = {-100, 1, 2, 5, 7, 2, 9};
    Heapsort(A);
    //System.out.println(A.length);
    for (int x = 1; x < A.length; x++) {
        System.out.println(A[x] + " ");
    }
}

public static void Build_MAX_heap(int A[]) {
    int n = A.length;
    for (int i = n / 2; i >= 1; i--) {
        MAX_heapify(A, i);
    }
}

public static void MAX_heapify(int A[], int i) {
    int heapsize = A.length;
    int l = 2 * i;
    int r = 2 * i + 1;
    //find the largest of A[i].A[l],A[r]
    int largest = i;
    if (A[l] > A[largest]) {
        largest = l;
        if (A[l] > A[largest] && l <= heapsize) {
            largest = l;
        }
        if (r<=heapsize && A[r] > A[largest]) {     //modification here
            largest = r;
        }
        if (largest != i) {
            int temp = A[i];
            A[i] = A[largest];
            A[largest] = temp;
            MAX_heapify(A, largest);
        }
    }
}

public static void Heapsort(int A[]) {

    Build_MAX_heap(A);
    int heapsize = A.length;
    for (int last = heapsize; last >= 2; last--) {   
        int temp = A[1];
        A[1] = A[last];
        A[last] = temp;
        heapsize--;
        MAX_heapify(A,1);
    }
}

}

Comments

0

Variable r is set to:

int r = 2 * i + 1;

In first case of loop:

for (int i = n / 2; i >= 1; i--) {
    MAX_heapify(A, i);
}

i is n/2

so to function is passed:

MAX_heapify(A,n/2);

and r is 2 * (n/2) + 1 which is n+1

when you want to do this line

if (A[r] > A[largest] && r <= heapsize) {

then A[r] is A[n+1]=A[A.length+1] - this causes IndexOutOfBoundsException

1 Comment

Thank you guys for all your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.