Chinaunix首页 | 论坛 | 博客
  • 博客访问: 16232
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-08-14 08:56
文章分类
文章存档

2016年(2)

2015年(27)

我的朋友
最近访客

分类: C/C++

2015-08-14 08:59:42


点击(此处)折叠或打开

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

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