SuzumiyaSAMA 2017-01-26 02:32 采纳率: 92.3%
浏览 1211
已采纳

一个快排的测试程序,但是没办法实现,求解答

代码:

 #include <iostream>
#include <vector>
#include <cstdbool>
using namespace std;

bool SwitchInt (int& a, int &b){
    if (a == b) return true;
    a += b;
    b = a - b;
    a -= b;
    return true;
}

bool QuickSort (vector<int> &ivec, vector<int>::iterator small, vector<int>::iterator big){
    if (small == big) return true;
    vector<int>::iterator start = small;
    vector<int>::iterator end = big;
    vector<int>::iterator flag = small;
    while (small != big){
        if (*small > *flag){
            SwitchInt(*small, *flag);
            flag = small;
            small++;
            continue;
        }
        if (*big < *flag){
            SwitchInt(*big, *flag);
            flag = big;
            big--;
            continue;
        }
    }
    QuickSort(ivec, start, small);
    QuickSort(ivec, big, end);
    return true;
}

int main (void){
    vector<int> ivec;
    int num = 0;
    while (cin >> num){
        ivec.push_back(num);}
    QuickSort(ivec, ivec.begin(), ivec.end());
    for (int num: ivec)
        cout << num;
    return 0;
}

运行时有指针超界的错误,但是我检查不出来

  • 写回答

4条回答 默认 最新

  • GKatHere 2017-02-08 10:21
    关注
    
    //
    //  bool SwitchInt (int& a, int &b){
    //      if (a == b) return true;
    //      a += b;
    //      b = a - b;
    //      a -= b;
    //      return true;
    //  }
    //  标准SWAP方法, 比上面的更快
    void SwitchInt (int& a, int &b){
        int t =a;
        a=b;
        b=t;
    }
    
    //  vector<int> &ivec 参数无用,去掉
    //  bool 返回值无用,去掉
    //  big 使用标准库end语法
    /*bool*/ void QuickSort (/*vector<int> &ivec,*/ vector<int>::iterator small, vector<int>::iterator big)
    {
        if (small == big) return /*true*/;
        vector<int>::iterator start = small;
        vector<int>::iterator end = big;
        vector<int>::iterator flag = small;
        //随机选取flag, 将小于等于此值的放在左边,大于此值的放于右边
        for (small++; small != big; small++){
            if (*small <= *flag){
                SwitchInt(*small, *flag);
                flag++;
                SwitchInt(*small, *flag);
    
                //           flag = small;
                //           small++;
                //           continue;
            }
            //       if (*big < *flag){
            //           SwitchInt(*big, *flag);
            //           flag = big;
            //           big--; 
            //           continue;
            //       }
    
        };
    
    
        cout  << &*start << ' ' << &*flag << ' ' <<  '\n';
        //QuickSort(/*ivec,*/ start, small);
        QuickSort(/*ivec,*/ start, flag);
        // QuickSort(/*ivec,*/ big, end);
        QuickSort(/*ivec,*/ ++flag, end);
    
    
        return /*true*/;
    }
    
    
    
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        srand(time(0));
    
        vector<int> ivec;
        int num = 0;
        //     while (cin >> num){
        //         ivec.push_back(num);}
        for (int n=0; n<10; n++)
        {
            cout << (num = rand()) << " ";
            ivec.push_back(num);
        }
        cout << "\n";
    
        QuickSort(/*ivec,*/ ivec.begin(), ivec.end());
        for_each(ivec.begin(), ivec.end(),
            [](int num){ cout << num << " ";});
    
    
        getchar();
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!