当重复出现数字时,二分法遍历排序出现问题,,求助
代码如下:我输入的数字是不同值时:没有发生故障;
当输入两个相同的值时:发生故障;
#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;
}