#include <iostream>
using namespace std;
//在一个递增数组中,二分查找值相等的任意一个数字,返回下标位置
int SearchEqualValue(int* arr,int len,int value)
{
if(arr==NULL || len<=0)
return -1;
int start=0,end=len-1;
while(start<=end)
{
int mid=start+(end-start)/2;
if(arr[mid]==value)
return mid;
else if(arr[mid]<value)
start=mid+1;
else
end=mid-1;
}
return -1;
}
//在一个递增数组中,二分查找值相等的第一个数字,返回下标位置(版本一)
int SearchFirstEqualValue_1(int* arr,int len,int value)
{
if(arr==NULL || len<=0)
return -1;
int start=0,end=len-1;
while(start<=end)
{
int mid=start+(end-start)/2;
if(arr[mid]==value)
{
if(mid==start || arr[mid-1]!=value)
return mid;
else
end=mid-1;
}
else if(arr[mid]<value)
start=mid+1;
else
end=mid-1;
}
return -1;
}
//在一个递增数组中,二分查找值相等的第一个数字,返回下标位置(版本二)
int SearchFirstEqualValue_2(int* arr,int len,int value)
{
if(arr==NULL || len<=0)
return -1;
int start=0,end=len-1;
while(start<end)
{
int mid=start+(end-start)/2;
if(arr[mid]>=value)
{
end=mid;
}
else
start=mid+1;
}
if(start==end)
return start;
return -1;
}
//在一个递增数组中,二分查找值相等的最后一个数字,返回下标位置(版本一)
int SearchLastEqualValue_1(int* arr,int len,int value)
{
if(arr==NULL || len<=0)
return -1;
int start=0,end=len-1;
while(start<=end)
{
int mid=start+(end-start)/2;
if(arr[mid]==value)
{
if(mid==end || arr[mid+1]!=value)
return mid;
else
start=mid+1;
}
else if(arr[mid]<value)
start=mid+1;
else
end=mid-1;
}
return -1;
}
//在一个递增数组中,二分查找值相等的最后一个数字,返回下标位置(版本二)
int SearchLastEqualValue_2(int* arr,int len,int value)
{
if(arr==NULL || len<=0)
return -1;
int start=0,end=len-1;
while(start<end-1)
{
int mid=start+(end-start)/2;
if(arr[mid]<=value)
{
start=mid;
}
else
end=mid-1;
}
if(arr[end]==value)
return end;
else if(arr[start]==value)
return start;
return -1;
}
//在一个递增数组中,二分查找第一个大于给定值value的数字,返回下标位置
int SearchFirstMoreThanValue(int* arr,int len,int value)
{
if(arr==NULL || len<=0)
return -1;
int start=0,end=len-1;
while(start<end)
{
int mid=start+(end-start)/2;
if(arr[mid]>value)
{
end=mid;
}
else
start=mid+1;
}
if(start==end)
return start;
return -1;
}
//在一个递增数组中,二分查找最后一个小于给定值value的数字,返回下标位置
int SearchLastLessThanValue(int* arr,int len,int value)
{
if(arr==NULL || len<=0)
return -1;
int start=0,end=len-1;
while(start<end-1)
{
int mid=start+(end-start)/2;
if(arr[mid]<value)
{
start=mid;
}
else
end=mid-1;
}
if(arr[end]<value)
return end;
else if(arr[start]<value)
return start;
return -1;
}
int main()
{
int arr1[]={1,3,3,5,7};
int arr2[]={1,2,3,3,5};
int arr3[]={1,2,3,3,3,6,7,8};
int arr4[]={3,3};
int arr5[]={3,4};
int arr6[]={2,3};
cout<<SearchFirstEqualValue_1(arr1,5,3)<<endl;
cout<<SearchFirstEqualValue_1(arr2,5,3)<<endl;
cout<<SearchFirstEqualValue_1(arr3,8,3)<<endl;
cout<<SearchFirstEqualValue_1(arr4,2,3)<<endl;
cout<<SearchFirstEqualValue_1(arr5,2,3)<<endl;
cout<<SearchFirstEqualValue_1(arr6,2,3)<<endl;
cout<<endl;
cout<<SearchFirstEqualValue_2(arr1,5,3)<<endl;
cout<<SearchFirstEqualValue_2(arr2,5,3)<<endl;
cout<<SearchFirstEqualValue_2(arr3,8,3)<<endl;
cout<<SearchFirstEqualValue_2(arr4,2,3)<<endl;
cout<<SearchFirstEqualValue_2(arr5,2,3)<<endl;
cout<<SearchFirstEqualValue_2(arr6,2,3)<<endl;
cout<<endl;
cout<<SearchLastEqualValue_1(arr1,5,3)<<endl;
cout<<SearchLastEqualValue_1(arr2,5,3)<<endl;
cout<<SearchLastEqualValue_1(arr3,8,3)<<endl;
cout<<SearchLastEqualValue_1(arr4,2,3)<<endl;
cout<<SearchLastEqualValue_1(arr5,2,3)<<endl;
cout<<SearchLastEqualValue_1(arr6,2,3)<<endl;
cout<<endl;
cout<<SearchLastEqualValue_2(arr1,5,3)<<endl;
cout<<SearchLastEqualValue_2(arr2,5,3)<<endl;
cout<<SearchLastEqualValue_2(arr3,8,3)<<endl;
cout<<SearchLastEqualValue_2(arr4,2,3)<<endl;
cout<<SearchLastEqualValue_2(arr5,2,3)<<endl;
cout<<SearchLastEqualValue_2(arr6,2,3)<<endl;
cout<<endl;
cout<<SearchFirstMoreThanValue(arr1,5,2)<<endl;
cout<<SearchFirstMoreThanValue(arr2,5,4)<<endl;
cout<<SearchFirstMoreThanValue(arr3,8,5)<<endl;
cout<<SearchFirstMoreThanValue(arr4,2,2)<<endl;
cout<<SearchFirstMoreThanValue(arr5,2,2)<<endl;
cout<<SearchFirstMoreThanValue(arr6,2,1)<<endl;
cout<<endl;
cout<<SearchLastLessThanValue(arr1,5,2)<<endl;
cout<<SearchLastLessThanValue(arr2,5,4)<<endl;
cout<<SearchLastLessThanValue(arr3,8,5)<<endl;
cout<<SearchLastLessThanValue(arr4,2,2)<<endl;
cout<<SearchLastLessThanValue(arr5,2,2)<<endl;
cout<<SearchLastLessThanValue(arr6,2,1)<<endl;
cout<<endl;
return 0;
}
二分查找算法(C++实现)
最新推荐文章于 2024-04-03 15:33:19 发布