堆排序和快速排序

来源:互联网 发布:chrome ubuntu 16.04 编辑:程序博客网 时间:2024/05/28 23:09

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. typedef struct
  3. {
  4.   int key;
  5. }keytype;
  6. typedef struct
  7. { keytype r[100];
  8.    int length;
  9. }sqlist;
  10. /*创建顺序表*/
  11. void creat(sqlist *l)
  12. {
  13.  int i,key;
  14.  printf("please intput it's length:");
  15.  scanf("%d",&l->length);
  16.  printf("\n\nplease intput %d data\n",l->length);
  17.  for(i=1;i<=l->length;i++)
  18.  {
  19.   scanf("%d",&key);
  20.   l->r[i].key=key;
  21.  }
  22. }

  23. /*显示顺序表*/
  24. void display(sqlist *l)
  25. { int i;
  26.    for(i=1;i<=l->length;i++)
  27.      printf("%-4.2d",i);
  28.    printf("\n");
  29.    for(i=1;i<=2*l->length;i++)
  30.      printf("--");
  31.    printf("\n");
  32.    for(i=1;i<=l->length;i++)
  33.      printf("%-4.2d",l->r[i].key);
  34. }
  35. /*调整h->r[s]的关键字,使h->r[s]成为一个大顶堆*/
  36. void heapadjust(sqlist *h,int s,int m)
  37. { keytype rc;
  38.     int j;
  39.     rc=h->r[s];
  40.     for(j=2*s;j<=m;j*=2)
  41.     { if(j<m&&h->r[j].key<h->r[j+1].key)
  42.          ++j;
  43.        if(rc.key>=h->r[j].key) break;
  44.        h->r[s]=h->r[j];
  45.        s=j;
  46.     }
  47.     h->r[s]=rc;
  48. }
  49. /*对顺序表h进行堆排序*/
  50. void heapsort(sqlist *h)
  51. { keytype rc;int i;
  52.    for(i=h->length/2;i>0;--i)
  53.      heapadjust(h,i,h->length);
  54.    for(i=h->length;i>1;--i)
  55.    { rc=h->r[1];
  56.       h->r[1]=h->r[i];
  57.       h->r[i]=rc;
  58.       heapadjust(h,1,i-1);
  59.    }
  60. }
  61. /*主函数*/
  62. void main()
  63. { sqlist t;int i;
  64.    creat(&t);
  65.    printf("\n\n");
  66.    heapsort(&t);
  67.    printf("\n");
  68.    printf("\nheapsort means:\n");
  69.    display(&t);
  70.    getch();
  71. }

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. typedef struct
  3. {
  4.   int key;
  5. }keytype;
  6. typedef struct
  7. { keytype r[100];
  8.    int length;
  9. }sqlist;
  10. /*创建顺序表*/
  11. void creat(sqlist *l)
  12. {
  13.  int i,key;
  14.  printf("please intput it's length:");
  15.  scanf("%d",&l->length);
  16.  printf("\n\nplease intput %d data\n",l->length);
  17.  for(i=1;i<=l->length;i++)
  18.  {
  19.   scanf("%d",&key);
  20.   l->r[i].key=key;
  21.  }
  22. }
  23. /*交换顺序表中子表r[low...high]的记录,使枢轴记录到位,并返回其所在的位置*/
  24. int partion(sqlist *l,int low,int high)
  25. { int pivotkey;
  26.     l->r[0]=l->r[low];
  27.     pivotkey=l->r[low].key;
  28.     while(low<high)
  29.     { while(low<high&&l->r[high].key>=pivotkey)
  30.       --high;
  31.       l->r[low]=l->r[high];
  32.       while(low<high&&l->r[low].key<=pivotkey)
  33.       ++low;
  34.       l->r[high]=l->r[low];
  35.     }
  36.     l->r[low]=l->r[0];
  37.     return low;
  38. }
  39. /*快速排序*/
  40. void Qsort(sqlist *l,int low,int high)
  41. { int pivotloc;
  42.   if(low<high)
  43.    { pivotloc=partion(l,low,high);
  44.       Qsort(l,low,pivotloc-1);
  45.       Qsort(l,pivotloc+1,high);
  46.    }
  47. }
  48. /*显示顺序表*/
  49. void display(sqlist *l)
  50. { int i;
  51.    for(i=1;i<=l->length;i++)
  52.      printf("%-4.2d",i);
  53.    printf("\n");
  54.    for(i=1;i<=2*l->length;i++)
  55.      printf("--");
  56.    printf("\n");
  57.    for(i=1;i<=l->length;i++)
  58.      printf("%-4.2d",l->r[i].key);
  59. }

  60. /*主函数*/
  61. void main()
  62. { sqlist t;int i;
  63.    creat(&t);
  64.    Qsort(&t,1,t.length);
  65.    printf("\n\n");
  66.    printf("quicksort means:\n");
  67.    display(&t);
  68.    getch();
  69. }

阅读(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,全国性价比最高

回复 | 举报
原创粉丝点击