运行时发生Java heapsort程序错误

运行时发生Java heapsort程序错误,java,heapsort,Java,Heapsort,我的代码在编译期间工作,但运行时错误指向我的MaxHeapify函数中的if条件。我把它标出来了。任何帮助都会很可爱,而且会让我不再把头撞在墙上 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);

我的代码在编译期间工作,但运行时错误指向我的
MaxHeapify
函数中的if条件。我把它标出来了。任何帮助都会很可爱,而且会让我不再把头撞在墙上

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);
        }
    }
}
公共类HeapSort{
公共静态void main(字符串[]args){
INTA[]={-100,1,2,5,7,2,9};
希普索尔(A);
//系统输出打印长度(A.长度);
对于(int x=1;x=1;i--){
马克斯·希皮菲(A,i);
}
}
公共静态void MAX_heapify(int A[],int i){
int heapsize=A.length;
int l=2*i;
int r=2*i+1;
//求A[i].A[l]和A[r]中的最大值
int=i;
如果(A[l]>A[最大]){
最大=l;
如果(A[l]>A[max]&&l A[max]&&r=2;最后--){
int temp=A[1];
A[1]=A[最后];
A[最后]=临时;
医治--;
MAX_heapify(A,1);
}
}
}

在检查索引
r
处的元素后,您正在对
r
进行边界检查(在前面的语句中也是如此)。
l
。如果r超出边界,表达式的前半部分将抛出异常,并且永远不会进行边界检查

您的if语句应该是结构化的

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

这样,在开始尝试访问数组之前,您就知道自己在边界内。此外,由于数组的索引为零,因此
heapsize
的索引无效,因此
r
需要小于、不小于或等于。

变量
r
设置为:

int r = 2 * i + 1;
在循环的第一种情况下:

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

因此,要传递函数:

MAX_heapify(A,n/2);
r
2*(n/2)+1
这是
n+1

当你想做这行的时候

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

if(A[r]>A[max]&&r您的问题是在检查r是否超出范围之前先检查A[r]
因此,我将尝试以这种方式修改代码

 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);
    }
}

}
公共类HeapSort{
公共静态void main(字符串[]args){
INTA[]={-100,1,2,5,7,2,9};
希普索尔(A);
//系统输出打印长度(A.长度);
对于(int x=1;x=1;i--){
马克斯·希皮菲(A,i);
}
}
公共静态void MAX_heapify(int A[],int i){
int heapsize=A.length;
int l=2*i;
int r=2*i+1;
//求A[i].A[l]和A[r]中的最大值
int=i;
如果(A[l]>A[最大]){
最大=l;
如果(A[l]>A[maxest]&&l=2;last--){
int temp=A[1];
A[1]=A[最后];
A[最后]=临时;
医治--;
MAX_heapify(A,1);
}
}
}

嗯,是什么错误?谢谢你们的帮助!