标题:当重复出现数字时,二分法遍历排序出现问题,,求助
只看楼主
luoye1994
Rank: 2
等 级:论坛游民
帖 子:57
专家分:58
注 册:2015-6-29
结帖率:100%
 问题点数:0 回复次数:0 
当重复出现数字时,二分法遍历排序出现问题,,求助
代码如下:
   我输入的数字是不同值时:没有发生故障;
   当输入两个相同的值时:发生故障;


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

class Line_1
{
private:
    int _size;
    vector<int>seq;
public:
    Line_1(vector<int>&tt);
    void List();
    void Print();
};
Line_1::Line_1(vector<int>&tt)
{
    for(int i = 0 ;i < tt.size(); ++i)
    {
        seq.push_back(tt[i]);
    }
    _size = tt.size();
}
void Line_1::List()
{
    int temp_seq;
    for(int i = 0 ; i < seq.size(); ++i)
    {
        int left = 0;
        int right = i;
        int mid;
        temp_seq = seq[i];
        while(left < right)
        {
            mid = (left + right)/2;
            if(temp_seq > seq[mid])
            {
                left = mid + 1;
            }
            else
            {
                right = mid - 1;
            }

        }
        for(int j = i ; j > left ; --j)
        {
            seq[j] = seq[j-1];
        }

        seq[left] = temp_seq;


    }
}
void Line_1::Print()
{
    for(int i = 0 ; i < _size ; ++i)
        cout << seq[i] << " ";
}

int main()
{
    int Size = 5;
    int a[Size] = {1,2,9,9,8};
    vector<int>tt(a , a+Size);
    Line_1 lin(tt);
    lin.List();
    lin.Print();

    return 0;
}
搜索更多相关主题的帖子: private include public 二分法 
2016-09-20 16:34



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-468979-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013930 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved