Skip to content

Commit

Permalink
Merge pull request huihut#50 from shoaibrayeen/patch-1
Browse files Browse the repository at this point in the history
Converting BS in Standard Form
  • Loading branch information
huihut authored Sep 7, 2019
2 parents 28daee4 + 18c3262 commit 69d7357
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions Algorithm/BinarySearch.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
// 二分查找(折半查找):对于已排序,若无序,需要先排序

// 非递归
int BinarySearch(vector<int> v, int value)
{
if (v.size() <= 0)
return -1;

int low = 0;
int high = v.size() - 1;
while (low <= high)
{
int BinarySearch(vector<int> v, int value , int low, int high) {
if (v.size() <= 0) {
return -1;
}
while (low <= high) {
int mid = low + (high - low) / 2;
if (v[mid] == value)
if (v[mid] == value) {
return mid;
else if (v[mid] > value)
}
else if (v[mid] > value) {
high = mid - 1;
else
}
else {
low = mid + 1;
}
}

return -1;
Expand All @@ -34,4 +34,4 @@ int BinarySearch2(vector<int> v, int value, int low, int high)
return BinarySearch2(v, value, low, mid - 1);
else
return BinarySearch2(v, value, mid + 1, high);
}
}

0 comments on commit 69d7357

Please sign in to comment.