堆排序

特点: 是一颗完全树(每层满的,且最底层的节点在左边);父节点数据均大于(小于)子女节点数据,为最大堆(最小堆)。

STL中堆的算法:

在<algorithm>库中。算法有:
1.make_heap(begin,end)//把从begin至end—1的序列换成堆;

2.push_heap(begin,end)//堆插入操作

3.pop_heap(begin,end);//堆删除操作,删除第一个元素,保持堆性质

4.sort_heap(begin,end);//使用堆排序来对堆进行排序

堆排序程序:完成了升序排序及降序排序

// 堆排序.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
const int N=30;
const int MAX_RAND=100;
const int MIN_RAND=20;
void swap(int& x,int& y);
void build_min_heap(int *heap,int length);//建最小堆
void build_max_heap(int *heap,int length);//建最大堆
void max_heap(int *heap,int index,int length);//维护最大堆
void min_heap(int *heap,int index,int length);//维护最小堆
void min_heap_sort(int *heap,int length);//最小堆排序
void max_heap_sort(int *heap,int length);//最大堆排序
void random_init(int *heap);  //随机产生数组
void swap(int& x,int& y)
{
 int temp=x;
 x=y;
 y=temp;
}
void max_heap(int *heap,int index,int length)
{
 int maxindex=2*index;
 if((2*index+1)>length)
  return;
 if(heap[2*index]<heap[2*index+1])
 {
   maxindex=2*index+1;
 }
 if(heap[index]<heap[maxindex])
 {
  swap(heap[index],heap[maxindex]);
  max_heap(heap, maxindex,length);
 }

}

void build_max_heap(int *heap,int length)
{
  for(int index=length/2;index>0;index--)
  {
   max_heap(heap,index,length);
  }
}
void min_heap(int *heap,int index,int length)
{
 int minindex=2*index;
 if((2*index+1)>length)
  return;
 if(heap[2*index]>heap[2*index+1])
  minindex=2*index+1;
 if(heap[index]>heap[minindex])
 {
  swap(heap[index],heap[minindex]);
  min_heap(heap,minindex,length);
 }
}
void build_min_heap(int *heap,int length)
{
  for(int index=length/2;index>0;index--)
  {
     min_heap(heap,index,length);
  }
}
void min_heap_sort(int *heap,int length)
{
 if(length<2)
  return;
 swap(heap[1],heap[length]);
 min_heap(heap,1,length-1);
 min_heap_sort(heap,length-1);

}
void max_heap_sort(int *heap,int length)
{
 if(length<2)
  return;
 swap(heap[1],heap[length]);
 max_heap(heap,1,length-1);
 max_heap_sort(heap,length-1);
}
void random_init(int *heap)  //随机产生数组
{  
 int i;  
    srand((unsigned)time(NULL));  
    for (i=1; i <N+1; i++)  
    {    
           heap[i]=rand() % MAX_RAND + MIN_RAND;         //随机生成数的范围.MIN_RAND ~ MAX_RAND.   
    }
}
void main()
{
 int heap[N+1];
 random_init(heap);
 cout<<"输出原始数组"<<endl;
 for(int i=1;i<N+1;i++)
 {
     cout<<heap[i]<<" ";
  if(i%10==0)
   cout<<endl;
 }
 build_min_heap(heap,N);
 min_heap_sort(heap,N);
 
 cout<<"输出最小堆排序"<<endl;
 for(int i=1;i<N+1;i++)
 {
     cout<<heap[i]<<" ";
  if(i%10==0)
   cout<<endl;
 }
 build_max_heap(heap,N);
 max_heap_sort(heap,N);
 cout<<"输出最大堆排序"<<endl;
 for(int i=1;i<N+1;i++)
 {
     cout<<heap[i]<<" ";
  if(i%10==0)
   cout<<endl;
 }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值