Some Different Conditions in Binary Search
Some Different Condition in Binary Search
Given a sort array, we can use binary search to identify the target in \(log_n\), but there are still three condition, let's see.
Given the Array[] with size \(n\), it's in the increasing order.
Identify the Index of the target that in the Array
In the basic condition, we want to identify the index of the target in the Array, if the target isn't in the Array, we get \(-1\).
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
int Binary_Search_Normal(vector<int>& Array, int target) {
int low = 0, high = Array.size() - 1;
while (low <= high) {
int middle = (low + high) >> 1;
if (Array[middle] > target) {
high = middle - 1;
}
else if (Array[middle] < target) {
low = middle + 1;
}
else {
return middle;
}
}
return -1;
}
};
int main(int argc, char const* argv[])
{
vector<int> Array = { 1,2,3,5,6,9,12,14,19,23,24,27,32 };
Solution Test;
int result;
result = Test.Binary_Search_Normal(Array, 19);
cout << result << endl;
return 0;
}
In this condition, in the binary part, we direct decrease the \(high\) and increase \(low\), and the loop condition is \(low \leq high\).
The last number that \(\leq\) target
The second condition is not directly find the target, sometimes target is not in the Array, so, we find the last number that \(\leq\) target.
In this part, we can first change the loop condition, because we know Array[high] must \(\geq\) Array[low], we also should take care of the \(low\), in the condition
Array[middle] < target
we should not increase the \(low\), because this might make \(Array[low] \geq target\), and we know Array[high] must \(\geq\) Array[low], In this condition, both Array[low] and Array[high] \(\geq\) target, som we can't arrive the index that Array[index] \(\leq\) target.
int Binary_Search_leq(vector<int>& Array, int target) {
int low = 0, high = Array.size() - 1;
while (Array[high] > target) {
// Attention!!! special condition,
if (low == high-1 && low <= target) {
return left;
}
int middle = (low + high) >> 1;
if (Array[middle] > target) {
high = middle - 1;
}
else if (Array[middle] < target) {
low = middle;
}
else {
return middle;
}
}
return high;
}
Actually, in this condition, we can easily change to The last number that \(<\) target.
The first number that \(\geq\) target
The last condition is not directly find the target, sometimes target is not in the Array, so, we find the last number that \(\geq\) target.
In this part, we can first change the loop condition, because we know Array[high] must \(\geq\) Array[low], we also should take care of the \(high\), in the condition
Array[middle] > target
we should not decrease the \(high\), because this might make \(Array[high] \leq target\), and we know Array[high] must \(\geq\) Array[low], In this condition, both Array[low] and Array[high] \(\leq\) target, so, we can't arrive the index that Array[index] \(\geq\) target.
int Binary_Search_leq(vector<int>& Array, int target) {
int low = 0, high = Array.size() - 1;
while (Array[low] < target) {
if(low == right-1 && right >= target) {
return right;
}
int middle = (low + high) >> 1;
if (Array[middle] > target) {
high = middle;
}
else if (Array[middle] < target) {
low = middle+1;
}
else {
return middle;
}
}
return low;
}
Actually, in this condition, we can easily change to The last number that \(>\) target.