Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7565175
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-05-31 10:42:48

  1. /*
  2.  *    递归方法实现快速排序
  3.  *    Lzy     2011-5-25
  4.  */
  5. #include <stdio.h>
  6. #define N 8

  7. int Quick_Sort_Partion(int R[],int left,int right)
  8. {                                            //用递归方法把R[left]至R[righ]的记录进行快速排序
  9.     int temp, buf;                            //临时变量
  10.     int i = left, j = right;

  11.     while(left < right)                            /* 从两端交替向中间扫描,直至low和high相等时为止 */
  12.     {
  13.         buf = R[left];                            //将区间的第1个记录作为基准 置入临时单元中
  14.         while(left < right && buf <= R[right])    /* 从右向左进行扫描,查找查找第1个小于基准值的数据元素*/
  15.             right--;
  16.     
  17.         temp = R[right];                        /* 交换数据元素x[low]和x[high] */
  18.         R[right] = R[left];
  19.         R[left] = temp;

  20.         while(left < right && buf >= R[left])    /* 从左向右进行扫描,查找查找第1个大于基准值的数据元素 */
  21.             left++;

  22.         if(left == right)
  23.             return left;
  24.         else
  25.         {
  26.             temp = R[right];                        /* 交换数据元素x[low]和x[high] */
  27.             R[right] = R[left];
  28.             R[left] = temp;
  29.         }
  30.     }
  31. }

  32. void Quick_Sort(int R[],int left,int right)                /* 定义快速排序函数 */
  33. {
  34.     int temp;

  35.     if(left < right)
  36.     {
  37.         temp = Quick_Sort_Partion(R, left, right);        /* 划分左、右子序列 */
  38.         Quick_Sort(R,left, temp-1);                        /* 递归调用,对左子序列进行快速排序 */
  39.         Quick_Sort(R,temp+1, right);                    /* 递归调用,对右子序列进行快速排序 */
  40.     }
  41. }

  42. /******测试程序*******/
  43. int main(void)
  44. {
  45.     int i;
  46.     int X[N] = {26,23,96,13,36,67,45,15};
  47.     Quick_Sort(X,0,7);
  48.     
  49.     for(i = 0; i < 8; i++)
  50.         printf("%d ",X[i]);
  51.     printf("\n");;
  52. }
阅读(8842) | 评论(0) | 转发(2) |
0

上一篇:简单排序实现

下一篇:链表排序

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