堆排序
来源:互联网 发布:支持向量机算法步骤 编辑:程序博客网 时间:2024/05/16 14:55
#include <iostream.h>
#include <malloc.h>
typedef struct
{
int length;
int *elem;
}HeapType;
void Create(HeapType &H)
{
int i;
cout<<"please input the length of the data:"<<endl;
cin>>H.length;
H.elem=(int *)malloc((H.length+1)*sizeof(int));
cout<<"please input the data:"<<endl;
for(i=1;i<=H.length;i++)
cin>>H.elem[i];
}
void Print(HeapType H)
{
int i;
for(i=1;i<=H.length;i++)
cout<<H.elem[i]<<" ";
cout<<endl;
}
void HeapAjust(HeapType &H,int s,int m)
{
int rc,j;
rc=H.elem[s];
for(j=2*s;j<=m;j*=2)
{
if(j<m && H.elem[j]<H.elem[j+1]) ++j;
if(!(rc<H.elem[j])) break;
H.elem[s]=H.elem[j];
s=j;
}
H.elem[s]=rc;
}
void HeapSort(HeapType &H)
{
int i;
for(i=H.length/2;i>0;--i)
HeapAjust(H,i,H.length);
for(i=H.length;i>1;--i)
{
H.elem[0]=H.elem[i];
H.elem[i]=H.elem[1];
H.elem[1]=H.elem[0];
HeapAjust(H,1,i-1);
}
}
void main()
{
HeapType H;
Create(H);
Print(H);
HeapSort(H);
Print(H);
}
#include <malloc.h>
typedef struct
{
int length;
int *elem;
}HeapType;
void Create(HeapType &H)
{
int i;
cout<<"please input the length of the data:"<<endl;
cin>>H.length;
H.elem=(int *)malloc((H.length+1)*sizeof(int));
cout<<"please input the data:"<<endl;
for(i=1;i<=H.length;i++)
cin>>H.elem[i];
}
void Print(HeapType H)
{
int i;
for(i=1;i<=H.length;i++)
cout<<H.elem[i]<<" ";
cout<<endl;
}
void HeapAjust(HeapType &H,int s,int m)
{
int rc,j;
rc=H.elem[s];
for(j=2*s;j<=m;j*=2)
{
if(j<m && H.elem[j]<H.elem[j+1]) ++j;
if(!(rc<H.elem[j])) break;
H.elem[s]=H.elem[j];
s=j;
}
H.elem[s]=rc;
}
void HeapSort(HeapType &H)
{
int i;
for(i=H.length/2;i>0;--i)
HeapAjust(H,i,H.length);
for(i=H.length;i>1;--i)
{
H.elem[0]=H.elem[i];
H.elem[i]=H.elem[1];
H.elem[1]=H.elem[0];
HeapAjust(H,1,i-1);
}
}
void main()
{
HeapType H;
Create(H);
Print(H);
HeapSort(H);
Print(H);
}
- 堆及堆排序
- 堆/堆排序特点
- 【二叉堆、堆排序】
- 二叉堆 & 堆排序
- 二叉堆 & 堆排序
- 堆与堆排序
- 堆与堆排序
- 堆与堆排序
- 堆与堆排序
- 堆与堆排序
- 堆与堆排序
- 堆和堆排序
- 堆排序(最大堆)
- 堆和堆排序
- 堆和堆排序
- 堆及堆排序
- 堆和堆排序
- 堆与堆排序
- Java多线程编程详解------------大神
- 传智播客成都java培训中心12月15日基础班开新班
- 行文本编辑算法思想(堆栈)!
- 系统升级时,数据库脚本执行注意事项,血的教训
- Activity生命周期
- 堆排序
- struts2 + ajax 用户名登录验证(struts2+spring+ibatis)
- 获取js的函数名称
- 微软等数据结构+算法面试100题全部答案集锦
- Django框架(二): 视图和URL配置
- shopex之phpunit环境搭建
- hdu 2152 之上下限母函数
- c++基础1:需要了解的几点重要概念
- 金蝶 K3 WISE k3web登录时提示 要执行些任务,您必须具有管理凭证 .