Chinaunix首页 | 论坛 | 博客
  • 博客访问: 616170
  • 博文数量: 87
  • 博客积分: 3399
  • 博客等级: 中校
  • 技术积分: 1422
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-17 21:20
文章分类

全部博文(87)

文章存档

2013年(1)

2012年(51)

2011年(33)

2010年(2)

分类: C/C++

2012-05-04 21:22:29


点击(此处)折叠或打开

  1. #include <iostream>
  2. using namespace std;
  3. const long MAXSIZE = 5000;
  4. typedef struct
  5. {
  6.     int r[MAXSIZE+1];
  7.     int length;
  8. }SqList;
  9. int partition(SqList &L, int low, int high)
  10. {
  11.     int count = 0;
  12.     L.r[0] = L.r[low];
  13.     int pivotkey = L.r[low];
  14.     while(low < high)
  15.     {
  16.         while(low < high && L.r[high] > pivotkey) --high;
  17.         {L.r[low] = L.r[high]; count++;}
  18.         while(low < high && L.r[low] < pivotkey) ++low;
  19.         {L.r[high] = L.r[low]; count++;}
  20.     }
  21.     L.r[low] = L.r[0];
  22.     cout<<"-----"<<count<<endl;
  23.     return low;
  24. }
  25. void qSort(SqList &L, int low, int high)
  26. {
  27.     /*
  28.     if(low < high)
  29.     {
  30.         int pivotloc = partition(L, low, high);
  31.         qSort(L, low, pivotloc-1);
  32.         qSort(L, pivotloc+1, high);
  33.     }
  34.     */
  35.     //用while循环消去尾递归
  36.     while(low < high)
  37.     {
  38.         int pivotloc = partition(L, low, high);
  39.         qSort(L, low, pivotloc-1);
  40.         low = pivotloc+1;
  41.     }
  42. }
  43. int main()
  44. {
  45.     SqList list;
  46.     list.length = 6;
  47.     list.r[0] = 0;
  48.     list.r[1] = 2;
  49.     list.r[2] = 1;
  50.     list.r[3] = 4;
  51.     list.r[4] = 5;
  52.     list.r[5] = 3;
  53.     qSort(list, 1, 5);
  54.     for(int i=1; i<list.length; i++)
  55.         cout<<list.r[i]<<endl;
  56.     return 0;
  57. }

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