要求:时间复杂度为O(log n)
简述算法过程:
采用二分查找,设置一个数值,用二分查找查这个数,如果可以查到证明有序,左边数大于右边数为降序,右边数大于左边数为升序。
class Env
{
public:
int data[40000];
int pointer_max = 0;
int pointer = 0;
Env()
{
// test_3.csv test_4.csv test_5.csv 为第二题的测试用例
ifstream file("test_data/test_1.csv", ios::in);
if (!file.is_open())
cout << " 文件打开失败!" << endl;
string temp;
while (getline(file, temp))
{
data[pointer_max] = atoi(temp.c_str());
pointer_max++;
}
file.close();
}
};
// 所有的代码都需要写在 Algo 类内,自行定义其他需要的变量或函数
class Algo
{
public:
Algo()
{
cout << "algo ok" << endl;
}
int osearch(int n[],int object,int m)
{
int left=0;
int right=m;
while(left<=right)
{
int mid=(right+left)/2;
if(n[left]object)
right=mid-1;
}
else{
cout<
结果:



