堆排序和快速排序
2013-09-12 19:29
190 查看
点击(此处)折叠或打开
#include<stdio.h>
typedef struct
{
int key;
}keytype;
typedef struct
{ keytype r[100];
int length;
}sqlist;
/*创建顺序表*/
void creat(sqlist *l)
{
int i,key;
printf("please intput it's length:");
scanf("%d",&l->length);
printf("\n\nplease intput %d data\n",l->length);
for(i=1;i<=l->length;i++)
{
scanf("%d",&key);
l->r[i].key=key;
}
}
/*显示顺序表*/
void display(sqlist *l)
{ int i;
for(i=1;i<=l->length;i++)
printf("%-4.2d",i);
printf("\n");
for(i=1;i<=2*l->length;i++)
printf("--");
printf("\n");
for(i=1;i<=l->length;i++)
printf("%-4.2d",l->r[i].key);
}
/*调整h->r[s]的关键字,使h->r[s]成为一个大顶堆*/
void heapadjust(sqlist *h,int s,int m)
{ keytype rc;
int j;
rc=h->r[s];
for(j=2*s;j<=m;j*=2)
{ if(j<m&&h-
4000
>r[j].key<h->r[j+1].key)
++j;
if(rc.key>=h->r[j].key) break;
h->r[s]=h->r[j];
s=j;
}
h->r[s]=rc;
}
/*对顺序表h进行堆排序*/
void heapsort(sqlist *h)
{ keytype rc;int i;
for(i=h->length/2;i>0;--i)
heapadjust(h,i,h->length);
for(i=h->length;i>1;--i)
{ rc=h->r[1];
h->r[1]=h->r[i];
h->r[i]=rc;
heapadjust(h,1,i-1);
}
}
/*主函数*/
void main()
{ sqlist t;int i;
creat(&t);
printf("\n\n");
heapsort(&t);
printf("\n");
printf("\nheapsort means:\n");
display(&t);
getch();
}
点击(此处)折叠或打开
#include<stdio.h>
typedef struct
{
int key;
}keytype;
typedef struct
{ keytype r[100];
int length;
}sqlist;
/*创建顺序表*/
void creat(sqlist *l)
{
int i,key;
printf("please intput it's length:");
scanf("%d",&l->length);
printf("\n\nplease intput %d data\n",l->length);
for(i=1;i<=l->length;i++)
{
scanf("%d",&key);
l->r[i].key=key;
}
}
/*交换顺序表中子表r[low...high]的记录,使枢轴记录到位,并返回其所在的位置*/
int partion(sqlist *l,int low,int high)
{ int pivotkey;
l->r[0]=l->r[low];
pivotkey=l->r[low].key;
while(low<high)
{ while(low<high&&l->r[high].key>=pivotkey)
--high;
l->r[low]=l->r[high];
while(low<high&&l->r[low].key<=pivotkey)
++low;
l->r[high]=l->r[low];
}
l->r[low]=l->r[0];
return low;
}
/*快速排序*/
void Qsort(sqlist *l,int low,int high)
{ int pivotloc;
if(low<high)
{ pivotloc=partion(l,low,high);
Qsort(l,low,pivotloc-1);
Qsort(l,pivotloc+1,high);
}
}
/*显示顺序表*/
void display(sqlist *l)
{ int i;
for(i=1;i<=l->length;i++)
printf("%-4.2d",i);
printf("\n");
for(i=1;i<=2*l->length;i++)
printf("--");
printf("\n");
for(i=1;i<=l->length;i++)
printf("%-4.2d",l->r[i].key);
}
/*主函数*/
void main()
{ sqlist t;int i;
creat(&t);
Qsort(&t,1,t.length);
printf("\n\n");
printf("quicksort means:\n");
display(&t);
getch();
}
阅读(342) | 评论(1) | 转发(0) |
0
上一篇:有向图的深度和广度搜索
下一篇:ADO.net实现数据查询表的分页
相关热门文章
欢迎miyostudio26在ChinaUnix...
30岁女教师开宝马下乡教书 每...
ok6410的Nand Flash驱动
建筑工程管理软件信息管理的目...
罗一平:建设美术馆的理想并未...
test123
编写安全代码——小心有符号数...
使用openssl api进行加密解密...
一段自己打印自己的c程序...
sql relay的c++接口
怎么样找出BIND中查询并发量多...
可有人在实际的openstack生产...
如下makefile如何编写
sqlldr 参数配置
讨论一下各位所管理的mysql生...
给主人留下些什么吧!~~
ryysoft2012-06-21 16:05:12
精通C语言,首选锐英源,www.wisestudy.cn,全国性价比最高
回复 | 举报
评论热议
#include<stdio.h>
typedef struct
{
int key;
}keytype;
typedef struct
{ keytype r[100];
int length;
}sqlist;
/*创建顺序表*/
void creat(sqlist *l)
{
int i,key;
printf("please intput it's length:");
scanf("%d",&l->length);
printf("\n\nplease intput %d data\n",l->length);
for(i=1;i<=l->length;i++)
{
scanf("%d",&key);
l->r[i].key=key;
}
}
/*显示顺序表*/
void display(sqlist *l)
{ int i;
for(i=1;i<=l->length;i++)
printf("%-4.2d",i);
printf("\n");
for(i=1;i<=2*l->length;i++)
printf("--");
printf("\n");
for(i=1;i<=l->length;i++)
printf("%-4.2d",l->r[i].key);
}
/*调整h->r[s]的关键字,使h->r[s]成为一个大顶堆*/
void heapadjust(sqlist *h,int s,int m)
{ keytype rc;
int j;
rc=h->r[s];
for(j=2*s;j<=m;j*=2)
{ if(j<m&&h-
4000
>r[j].key<h->r[j+1].key)
++j;
if(rc.key>=h->r[j].key) break;
h->r[s]=h->r[j];
s=j;
}
h->r[s]=rc;
}
/*对顺序表h进行堆排序*/
void heapsort(sqlist *h)
{ keytype rc;int i;
for(i=h->length/2;i>0;--i)
heapadjust(h,i,h->length);
for(i=h->length;i>1;--i)
{ rc=h->r[1];
h->r[1]=h->r[i];
h->r[i]=rc;
heapadjust(h,1,i-1);
}
}
/*主函数*/
void main()
{ sqlist t;int i;
creat(&t);
printf("\n\n");
heapsort(&t);
printf("\n");
printf("\nheapsort means:\n");
display(&t);
getch();
}
点击(此处)折叠或打开
#include<stdio.h>
typedef struct
{
int key;
}keytype;
typedef struct
{ keytype r[100];
int length;
}sqlist;
/*创建顺序表*/
void creat(sqlist *l)
{
int i,key;
printf("please intput it's length:");
scanf("%d",&l->length);
printf("\n\nplease intput %d data\n",l->length);
for(i=1;i<=l->length;i++)
{
scanf("%d",&key);
l->r[i].key=key;
}
}
/*交换顺序表中子表r[low...high]的记录,使枢轴记录到位,并返回其所在的位置*/
int partion(sqlist *l,int low,int high)
{ int pivotkey;
l->r[0]=l->r[low];
pivotkey=l->r[low].key;
while(low<high)
{ while(low<high&&l->r[high].key>=pivotkey)
--high;
l->r[low]=l->r[high];
while(low<high&&l->r[low].key<=pivotkey)
++low;
l->r[high]=l->r[low];
}
l->r[low]=l->r[0];
return low;
}
/*快速排序*/
void Qsort(sqlist *l,int low,int high)
{ int pivotloc;
if(low<high)
{ pivotloc=partion(l,low,high);
Qsort(l,low,pivotloc-1);
Qsort(l,pivotloc+1,high);
}
}
/*显示顺序表*/
void display(sqlist *l)
{ int i;
for(i=1;i<=l->length;i++)
printf("%-4.2d",i);
printf("\n");
for(i=1;i<=2*l->length;i++)
printf("--");
printf("\n");
for(i=1;i<=l->length;i++)
printf("%-4.2d",l->r[i].key);
}
/*主函数*/
void main()
{ sqlist t;int i;
creat(&t);
Qsort(&t,1,t.length);
printf("\n\n");
printf("quicksort means:\n");
display(&t);
getch();
}
阅读(342) | 评论(1) | 转发(0) |
0
上一篇:有向图的深度和广度搜索
下一篇:ADO.net实现数据查询表的分页
相关热门文章
欢迎miyostudio26在ChinaUnix...
30岁女教师开宝马下乡教书 每...
ok6410的Nand Flash驱动
建筑工程管理软件信息管理的目...
罗一平:建设美术馆的理想并未...
test123
编写安全代码——小心有符号数...
使用openssl api进行加密解密...
一段自己打印自己的c程序...
sql relay的c++接口
怎么样找出BIND中查询并发量多...
可有人在实际的openstack生产...
如下makefile如何编写
sqlldr 参数配置
讨论一下各位所管理的mysql生...
给主人留下些什么吧!~~
ryysoft2012-06-21 16:05:12
精通C语言,首选锐英源,www.wisestudy.cn,全国性价比最高
回复 | 举报
评论热议
相关文章推荐
- 【C++】自己的快速排序与二分插入排序的封装
- 交换排序——快速排序
- 简单排序--快速排序
- C/C++实现快速排序和随机快速排序
- 排序之快速排序(quickSort)
- 小白排序之快速排序解读
- c++排序(快速排序)
- 排序(4) ---快速排序
- 简单的排序算法——插入排序,选择排序,交换排序(冒泡排序,快速排序)
- 快速排序的复杂度分析以及使用插入排序优化的快速排序
- 编程菜鸟的日记-初学尝试编程-整理内部排序(交换排序(冒泡排序和快速排序)、直接选择排序、直接插入排序,希尔排序)
- 排序算法之快速排序
- 快速排序、希尔排序、插入排序、选择排序、归并排序、堆排序总结
- 排序——快速排序
- 排序算法之快速排序(Quick sort)
- 排序--快速排序
- 排序——快速排序
- 交换类排序之快速排序
- HNUST 1395日期排序 sort函数 快速排序
- 排序(冒泡排序与快速排序)