Chinaunix首页 | 论坛 | 博客
  • 博客访问: 74171
  • 博文数量: 46
  • 博客积分: 560
  • 博客等级: 下士
  • 技术积分: 386
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-22 22:14
文章分类

全部博文(46)

文章存档

2013年(4)

2012年(42)

我的朋友

分类: C/C++

2012-06-21 14:52:57


点击(此处)折叠或打开

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

阅读(516) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

ryysoft2012-06-21 16:05:12

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