最大堆性质: A[PARENT(i)]>=A[i]
最大堆排序后是升序哦!
#include <iostream>
using namespace std;
#define N 10
void HEAP_SORT(int a[], int n);
void BUILD_MAX_HEAP(int a[], int n);
void MAX_HEAPIFY(int a[], int n, int i);
int HEAP_EXTRACT_MAX(int a[], int n);
void MAX_HEAP_INSERT(int a[], int n, int key);
int HEAP_INCREASE_KEY(int a[], int n, int i, int key);
int PARENT(int a[], int i);
void exchange(int a[], int i, int j);
int LEFT(int a[], int i);
int RIGHT(int a[], int i);
void PRINT(int a[], int n);
int heap_size=N;
int main()
{
//
数组第一个元素是占据下标为0的空间的。以便让正式的数据从下标1开始。。这样left(i)=2i rigth(i)=2i+1
// int a[N+1]={-12,4,1,3,2,16,9,10,14,8,7};
// int heap_size=N;
int a[N+1]={-12,16,4,10,14,7,9,3,2,8,1};
MAX_HEAPIFY(a,N+1,2);
PRINT(a,N+1);
// BUILD_MAX_HEAP(a,N+1);
/*
cout<<"begin: ";
PRINT(a,N+1);
HEAP_SORT(a,N+1);
cout<<"after: ";
PRINT(a,N+1);
*/
/*
int a[N+1]={-12,1,9,10,2,7,5,6,4,13,3,18,11,100};
cout<<"数组初始化:"<<endl;
PRINT(a,N+1);
cout<<"建立最大堆:"<<endl;
BUILD_MAX_HEAP(a,N+1);
PRINT(a,N+1);
cout<<"删除最大堆的当前最大元素:"<<endl;
HEAP_EXTRACT_MAX(a,N+1);
cout<<"数组长度: "<<sizeof(a)/sizeof(a[0])<<endl;
PRINT(a,N);
cout<<"最大堆第9位的数增加到15:"<<endl;
HEAP_INCREASE_KEY(a,N+1,9,15);
PRINT(a,N);
cout<<"最大堆插入一个元素56:"<<endl;
MAX_HEAP_INSERT(a,N+1,56);
PRINT(a,N+1);
cout<<"最大堆排序后的数组为:"<<endl;
HEAP_SORT(a,N+1);
PRINT(a,N+1);
*/
return 0;
}
int HEAP_MAXIMUM(int a[], int n){
return a[n-1];
}
int HEAP_EXTRACT_MAX(int a[], int n){
if(heap_size<1)
return -12;
int max1=a[1];
a[1]=a[heap_size];
heap_size--;
MAX_HEAPIFY(a,n,1);
return max1;
}
int HEAP_INCREASE_KEY(int a[], int n, int i, int key){
if(key<a[i])
return -12;
a[i]=key;
// cout<<i<<": "<<a[i]<<" "<<key<<endl;
while(i>1&&a[PARENT(a,i)]<a[i]){
// cout<<i<<":"<<a[i]<<" "<<PARENT(a,i)<<":"<<a[PARENT(a,i)]<<endl;
exchange(a,i,PARENT(a,i));
i=PARENT(a,i);
}
cout<<"i: "<<i<<endl;
return 0;
}
void MAX_HEAP_INSERT(int a[], int n, int key){
heap_size++;
cout<<"heap_size: "<<heap_size<<endl;
a[heap_size]=-999;
HEAP_INCREASE_KEY(a,n,heap_size,key);
}
void HEAP_SORT(int a[], int n){
// cout<<"after build: ";
// PRINT(a,n);
for(int i=n-1;i>=2;i--){
exchange(a,1,i);
heap_size-=1;
MAX_HEAPIFY(a,n,1);
}
}
void BUILD_MAX_HEAP(int a[], int n){
//在建堆中 HEAP_SIZE始终不变,初始时就根据输入的数据设定好了!!!for(int i=(n-1)/2;i>=1;i--){
MAX_HEAPIFY(a,n,i);
}
cout<<"after build: ";
cout<<endl;
PRINT(a,n);
}
/*
void MAX_HEAPIFY(int a[], int n, int i){
int l,r;
l=LEFT(a,i);
r=RIGHT(a,i);
int largest=-12;
if(l<=heap_size&&a[l]>a[i])
largest=l;
else
largest=i;
if(r<=heap_size&&a[r]>a[largest])
largest=r;
if(largest!=i&&largest!=-12){
exchange(a,i ,largest);
MAX_HEAPIFY(a,n,largest);
}
}
*/
void MAX_HEAPIFY(int a[], int n, int i){
while(i<=heap_size/2){
int l,r;
l=LEFT(a,i);
r=RIGHT(a,i);
int largest=-12;
if(l<=heap_size&&a[l]>a[i]){
largest=l;
}
else{
largest=i;
}
if(r<=heap_size&&a[r]>a[largest]){
largest=r;
}
cout<<"largest: "<<largest<<" a[largest]: "<<a[largest]<<" i: "<<i<<" a[i] "<<a[i]<<endl;
if(largest!=i){
exchange(a,i ,largest);
i=largest; //为什么不能放在这里!!!
}else{ //错误写法if(largest==i) 因为经过上面if语句后largest必然等于 i 啦!!!
return;
}
// i=largest;
cout<<"i "<<i<<" largest "<<largest<<endl;
}
}
int LEFT(int a[], int i){
return 2*i;
}
int RIGHT(int a[], int i){
return 2*i+1;
}
int PARENT(int a[], int i){
return i/2;
}
void exchange(int a[], int i, int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void PRINT(int a[], int n){
for(int i=1;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
参考: http://blog.csdn.net/z84616995z/article/details/17797393#reply