class HeapSort{
public static void main(String args[]){
int A[]={6,5,4,3,2,1};
HeapSor obj=new HeapSor(6);
obj.Heap_Sort(A);
obj.print(A);
}
}
class HeapSor{
int lenOfArray;
int HeapSize;
HeapSor(int len){
this.lenOfArray=len;
this.HeapSize=len;
}
void Heap_Sort(int A[]){
for(int i=0; i<lenOfArray; i++){
BuiltHeap(A);
HeapSize--;
swap(A,i,lenOfArray-i);
}
}
void BuiltHeap(int A[]){
for(int i=lenOfArray/2; i>=0; i--)
MaxHeapify(A,i);
}
void MaxHeapify(int A[],int i){
int l=2*i;
int r=2*i+1;
int max=i;
if(i>HeapSize)
return;
if(A[l]>A[r])
max=l;
else
max=r;
if(A[i]<A[max])
swap(A,i,max);
//max=i;
}
void swap(int A[],int i,int j){
if(i==j)
return;
int temp=A[i];
A[i]=A[j];
A[j]=temp;
}
void print(int A[]){
for(int i=0; i<lenOfArray; i++)
System.out.print(A[i]+" ");
System.out.println();
}
}
当我编译它给了我这个错误
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at HeapSor.MaxHeapify(HeapSort.java:41)
at HeapSor.BuiltHeap(HeapSort.java:31)
at HeapSor.Heap_Sort(HeapSort.java:23)
at HeapSort.main(HeapSort.java:5)
我真的很想知道现在出了什么问题,但我失败了请任何人告诉我我的问题是什么?
对不起,我的英语不好