堆排序和快速排序


点击(此处)折叠或打开

  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. }

<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(342) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

ryysoft2012-06-21 16:05:12

精通C语言,首选锐英源,www.wisestudy.cn,全国性价比最高

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值