# Binary search set

Keywords: less

I often use two points when I brush questions. Although I can write every time I stumble, I haven't made a summary. Today I met again. Let me summarize.

In an ordered array, find a number whose value is key and return its subscript. If there is more than one, return any one. If there is no one, return - 1;

```int binary_search(vector<int> a, int key){
int len = a.size();
int left = 0, right = len - 1, mid;
while(left <= right){
mid = (left + right) / 2;
if(a[mid] == key){
return mid;
}else if(a[mid] < key){
left = mid + 1;
}else{
right = mid - 1;
}
}
return -1;
}```

In an ordered array, find a number whose value is key, return the subscript of the number whose first value is greater than or equal to key, and return - 1 if it does not exist;

```int FirstGreaterOrEqualNumber(vector<int> a, int key){
int len = a.size();
int left = 0, right = len - 1, mid;
while(left <= right){
mid = (left + right) / 2;
if(a[mid] < key){
left = mid + 1;
}else{
right = mid - 1;
}
}
return left < len ? left : -1;
}
```

In an ordered array, find the subscript of the first number whose value is greater than key, and return - 1 if it does not exist;
The only difference from the previous code is that if judges that there are more =.

```int FirstGreaterNumber(vector<int> a, int key){
int len = a.size();
int left = 0, right = len - 1, mid;
while(left <= right){
mid = (left + right) / 2;
if(a[mid] <= key){
left = mid + 1;
}else{
right = mid - 1;
}
}
return left < len ? left : -1;
}```

In an ordered array, find the subscript of the first number whose value is less than key, and return - 1 if it does not exist;

```int FirstLessKeyNumber(vector<int> a, int key){
int len = a.size();
int left = 0, right = len - 1, mid;
while(left <= right){
mid = (left + right) / 2;
if(a[mid] < key){
left = mid + 1;
}else{
right = mid - 1;
}
}
return left - 1 < 0 ? -1 : left - 1;
}```

In an ordered array, find a number whose value is key, return the index of the number whose first value is equal to key, and return - 1 if it does not exist;

```int GetFirstIndexEqualKey(vector<int> a, int key){
int len = a.size();
int left = 0, right = len - 1, mid;
while(left <= right){
mid = (left + right) / 2;
if(a[mid] < key){
left = mid + 1;
}else{
right = mid - 1;
}
}
return (left < len && a[left] == key) ? left : -1;
}
```

In an ordered array, find a number whose value is key, return the subscript of the number whose last value is equal to key, and return - 1 if it does not exist;

```int GetLastIndexEqualKey(vector<int> a, int key){
int len = a.size();
int left = 0, right = len - 1, mid;
while(left <= right){
mid = (left + right) / 2;
if(a[mid] <= key){
left = mid + 1;
}else{
right = mid - 1;
}
}
return (left - 1 >= 0 && a[left - 1] == key) ? left - 1 : -1;
}```

In an ordered array, find the number of times a number with the value of key appears. If it does not exist, return - 1;
This can be combined with first finding the subscript of the number whose first value is key, then finding the subscript of the number whose first value is greater than the value key, and then subtracting the two subscripts.

Posted by Spaceboy on Thu, 30 Apr 2020 20:33:00 -0700