我有这些程序
#include <iostream>
using namespace std;
int parent(int i ){
return i/2;
}
int left(int i ){
return 2*i;
}
int right(int i){
return 2*i+1;
}
int a[]={ 27,17,3,16,10,1,5,7,12,4,8,9,10};
int n=sizeof(a)/sizeof(int);
void max_Heapify(int i){
int largest=0;
int l=left(i);
int r=right(i);
if(l<=n && a[l]>a[i]){
largest=l;
}
else{
largest=i;
}
if(r< n && a[r]>a[largest]){
largest=r;
}
if (largest!=i){
int t=a[i];
a[i]=a[largest];
a[largest]=t;
}
max_Heapify(largest);
}
int main(){
max_Heapify(2);
for (int i=0;i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}
当我运行它编译正常但运行停止后它运行异常为什么?请帮忙看看这段代码
Max-Heapify[2](A, i):
left ← 2i
right ← 2i + 1
largest ← i
if left ≤ heap-length[A] and A[left] > A[i] then:
largest ← left
if right ≤ heap-length[A] and A[right] > A[largest] then:
largest ← right
if largest ≠ i then:
swap A[i] ↔ A[largest]
Max-Heapify(A, largest)
来自维基百科。