堆排序 heapsort
2009-08-26 14:42
316 查看
以下代码包含三部分代码 1.MaxHeapIFY,BuidMaxHeap,HeapSort
2.HeapIncreaseKey,MaxHeapInsert,BuidMaxHeap2
其中 BuildMaxHeap采用 MaxHeapIFY作为子过程,BuildMaxHeap2采用 MaxHeapInsert作为子过程。两种构造最大堆的方法不同点在于前者是一种bottom-up 的方案:即保证某个二叉树是最大堆,那么首先他的左子树和右子树都是最大堆。后者是每插入一个节点,我们要保证该节点比他的父节点小。
前者的算法复杂度为O(n)后者为O(nlogn)。
我们可以看出即便对同一个数组,采用这两种方法构建最大堆,构造出的最大堆也是不一定相同的。 还有一点需要说明,C++语言的数组下标是从0开始的,但是算法的伪代码实现中数组下标是从1开始的。所以父母,左孩子,右孩子的计算公式会稍有不一致。
#include<iostream>
using namespace std;
#define Infinity -1000
/*求一个节点的左孩子,注意由于C语言中下标从0开始*/
int Left(int i,int heaplen)
{
if(2*i+1<heaplen)
return 2*i+1;
else
return 0;/*如果下标超过堆长,表明此节点没有左孩子*/
}
int Right(int i,int heaplen)
{
if(2*i+2<heaplen)
return 2*i+2;
else
return 0;/*如果下标超过堆长,表明此节点没有右孩子*/
}
void MaxHeapIFY(int a[],int n ,int heaplen,int i)
{
int l=Left(i,heaplen);
int r=Right(i,heaplen);
int max=i;
if(l!=0&&a[max]<a[l])
max=l;
if(r!=0&&a[max]<a[r])
max=r;
if(max!=i)
{
int temp=a[i];
a[i]=a[max];
a[max]=temp;
MaxHeapIFY(a,n,heaplen,max);
}
}
void BuildMaxHeap(int a[],int n,int heaplen)
{
for(int k=heaplen/2-1;k>=0;k--)
{
MaxHeapIFY(a,n,heaplen,k);
}
}
void HeapSort(int a[],int n,int heaplen)
{
BuildMaxHeap(a,n,heaplen);
while(heaplen)
{
int temp=a[heaplen-1];
a[heaplen-1]=a[0];
a[0]=temp;
heaplen--;
MaxHeapIFY(a,n,heaplen,0);
}
}
int Parent(int i)
{ if((i+1)/2)
return (i+1)/2-1;
else
return 0;
}
void HeapIncreaseKey(int a[],int i,int key)
{
if(key<a[i])
{
cout<<"error"<<endl;
return;
}
a[i]=key;
while(i>0&&a[i]>a[Parent(i)])
{
int temp=a[i];
a[i]=a[Parent(i)] ;
a[Parent(i)]=temp;
i=Parent(i);
}
}
void MaxHeapInsert(int a[],int n,int heaplen,int key)
{
heaplen+=1;
a[heaplen-1]=Infinity;
HeapIncreaseKey(a,heaplen-1,key);
}
void BuildMaxHeap2(int a[],int n)
{
int heaplen=1;
for(int i=1;i<n;i++)
{
MaxHeapInsert(a,n,heaplen,a[i]);
heaplen++;
}
}
void Print(int a[],int n)
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<";";
}
cout<<endl;
}
void main()
{
int a[7]={4,5,9,3,7,1,2};
//int b[7]={0,0,0,0,0,0,0};
// BuildMaxHeap(a,7,7);
// Print(a,7);
BuildMaxHeap2(a,7);
Print(a,7);
// HeapSort(a,7,7);
//Print(a,7);
}
2.HeapIncreaseKey,MaxHeapInsert,BuidMaxHeap2
其中 BuildMaxHeap采用 MaxHeapIFY作为子过程,BuildMaxHeap2采用 MaxHeapInsert作为子过程。两种构造最大堆的方法不同点在于前者是一种bottom-up 的方案:即保证某个二叉树是最大堆,那么首先他的左子树和右子树都是最大堆。后者是每插入一个节点,我们要保证该节点比他的父节点小。
前者的算法复杂度为O(n)后者为O(nlogn)。
我们可以看出即便对同一个数组,采用这两种方法构建最大堆,构造出的最大堆也是不一定相同的。 还有一点需要说明,C++语言的数组下标是从0开始的,但是算法的伪代码实现中数组下标是从1开始的。所以父母,左孩子,右孩子的计算公式会稍有不一致。
#include<iostream>
using namespace std;
#define Infinity -1000
/*求一个节点的左孩子,注意由于C语言中下标从0开始*/
int Left(int i,int heaplen)
{
if(2*i+1<heaplen)
return 2*i+1;
else
return 0;/*如果下标超过堆长,表明此节点没有左孩子*/
}
int Right(int i,int heaplen)
{
if(2*i+2<heaplen)
return 2*i+2;
else
return 0;/*如果下标超过堆长,表明此节点没有右孩子*/
}
void MaxHeapIFY(int a[],int n ,int heaplen,int i)
{
int l=Left(i,heaplen);
int r=Right(i,heaplen);
int max=i;
if(l!=0&&a[max]<a[l])
max=l;
if(r!=0&&a[max]<a[r])
max=r;
if(max!=i)
{
int temp=a[i];
a[i]=a[max];
a[max]=temp;
MaxHeapIFY(a,n,heaplen,max);
}
}
void BuildMaxHeap(int a[],int n,int heaplen)
{
for(int k=heaplen/2-1;k>=0;k--)
{
MaxHeapIFY(a,n,heaplen,k);
}
}
void HeapSort(int a[],int n,int heaplen)
{
BuildMaxHeap(a,n,heaplen);
while(heaplen)
{
int temp=a[heaplen-1];
a[heaplen-1]=a[0];
a[0]=temp;
heaplen--;
MaxHeapIFY(a,n,heaplen,0);
}
}
int Parent(int i)
{ if((i+1)/2)
return (i+1)/2-1;
else
return 0;
}
void HeapIncreaseKey(int a[],int i,int key)
{
if(key<a[i])
{
cout<<"error"<<endl;
return;
}
a[i]=key;
while(i>0&&a[i]>a[Parent(i)])
{
int temp=a[i];
a[i]=a[Parent(i)] ;
a[Parent(i)]=temp;
i=Parent(i);
}
}
void MaxHeapInsert(int a[],int n,int heaplen,int key)
{
heaplen+=1;
a[heaplen-1]=Infinity;
HeapIncreaseKey(a,heaplen-1,key);
}
void BuildMaxHeap2(int a[],int n)
{
int heaplen=1;
for(int i=1;i<n;i++)
{
MaxHeapInsert(a,n,heaplen,a[i]);
heaplen++;
}
}
void Print(int a[],int n)
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<";";
}
cout<<endl;
}
void main()
{
int a[7]={4,5,9,3,7,1,2};
//int b[7]={0,0,0,0,0,0,0};
// BuildMaxHeap(a,7,7);
// Print(a,7);
BuildMaxHeap2(a,7);
Print(a,7);
// HeapSort(a,7,7);
//Print(a,7);
}
相关文章推荐
- 堆排序(Heap Sort)
- PHP实现排序堆排序(Heap Sort)算法
- 堆排序(Heap Sort)算法学习
- Atitit 算法之道 attilax著 1. 第二部分(Part II) 排序与顺序统计(Sorting and Order Statistics) 1 2. 第六章 堆排序(Heapsort)
- DSOJ Heapsort implementation(堆排序)
- 经典排序算法 - 堆排序Heap sort
- 堆排序(heap_sort)的两种方法
- 算法----堆排序(heap sort)
- 选择排序算法:堆排序-Heap Sort
- 堆排序(heap Sort)
- 堆排序(Heap Sort)算法的实现
- 【排序算法】 堆排序 heap sort(选择类排序)
- algorithm: heap sort in python 算法导论 堆排序
- 堆排序(Heapsort)之Java实现
- 堆排序(heap sort)
- 算法导论------堆排序heapsort
- 堆排序——HeapSort
- PHP排序算法之堆排序(Heap Sort)实例详解
- Java排序算法(二)--堆排序(HeapSort)
- 堆排序练习(Heap Sort)