Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5464
  • 博文数量: 7
  • 博客积分: 166
  • 博客等级: 入伍新兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-30 18:08
文章分类
文章存档

2011年(7)

我的朋友
最近访客

分类: C/C++

2011-04-17 20:03:14

总算自己写了一遍!!

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void Qsort(int p[],int low,int high);
  4. int Partition(int p[],int low,int high);
  5. void Swap(int *a,int *b);
  6. int main(void){
  7.     int n;
  8.     int *p; //动态数组的声明详见p220
  9.     puts("输入你要输入数字的个数:");
  10.     scanf("%d",&n);
  11.     p = (int*)malloc(n*sizeof(int));
  12.     for(int i=0;i<n;i++)
  13.     {
  14.             scanf("%d",&p[i]);
  15.             }
  16.     Qsort(p,0,n-1);
  17.     for(int i=0;i<n;i++)
  18.     {
  19.             printf("%d\t",p[i]);
  20.             }
  21.     free(p);
  22.     system("PAUSE");
  23.     return 0;
  24.     }
  25.     
  26. void Qsort(int p[],int low,int high)
  27.    {
  28.      if(low < high)
  29.      {
  30.             int pivotLoc = Partition(p,low,high);
  31.             Qsort(p,low,pivotLoc-1);
  32.             Qsort(p,pivotLoc+1,high);
  33.      }
  34.     }
  35. int Partition(int p[],int low,int high)
  36. {
  37.     while(low<high)
  38.     {
  39.         while(low<high && p[high]>=p[low])
  40.         {
  41.               high--;
  42.         }
  43.         Swap(&p[low],&p[high]);
  44.         while(low<high && p[low]<=p[high])
  45.         {
  46.                low++;
  47.         }
  48.         Swap(&p[low],&p[high]);
  49.      }
  50.         return low;
  51. }
  52. void Swap(int *a,int *b){
  53.          int temp;
  54.          temp = *a;
  55.          *a = *b;
  56.          *b = temp;
  57.          }
阅读(390) | 评论(0) | 转发(0) |
0

上一篇:赛门铁克面试总结

下一篇:反转字符串

给主人留下些什么吧!~~