Converting BS in Standard Form

This commit is contained in:
Shoaib Rayeen 2019-09-06 13:46:39 +05:30 committed by GitHub
parent 28daee4bb3
commit 18c3262442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,23 +1,23 @@
// 二分查找(折半查找):对于已排序,若无序,需要先排序 // 二分查找(折半查找):对于已排序,若无序,需要先排序
// 非递归 // 非递归
int BinarySearch(vector<int> v, int value)
{
if (v.size() <= 0)
return -1;
int low = 0; int BinarySearch(vector<int> v, int value , int low, int high) {
int high = v.size() - 1; if (v.size() <= 0) {
while (low <= high) return -1;
{ }
while (low <= high) {
int mid = low + (high - low) / 2; int mid = low + (high - low) / 2;
if (v[mid] == value) if (v[mid] == value) {
return mid; return mid;
else if (v[mid] > value) }
else if (v[mid] > value) {
high = mid - 1; high = mid - 1;
else }
else {
low = mid + 1; low = mid + 1;
} }
}
return -1; return -1;
} }