-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBinarySearchAlgorithms.cpp
35 lines (35 loc) · 1.83 KB
/
BinarySearchAlgorithms.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
//Three O(LogN) algorithms based on the same dividing strategy
//Binary search returns the index of the search element
//Upper_Bound returns the index of the first element strictly greater than the search element
//Lower_Bound returns the index of the first element greater than or equal to the search element
int BinarySearch(int LowerBound, int UpperBound, int SearchElement, int Array[]){
int MidPoint = (LowerBound + UpperBound)/2;
if(UpperBound<LowerBound) return -1;
if(Array[MidPoint]==SearchElement) return MidPoint;
if(Array[MidPoint]>SearchElement) return BinarySearch(LowerBound, MidPoint-1, SearchElement, Array);
if(Array[MidPoint]<SearchElement) return BinarySearch(MidPoint+1, UpperBound, SearchElement, Array);
}
int Upper_Bound(int LowerBound, int UpperBound, int SearchElement, int Array[]){
int MidPoint = (LowerBound + UpperBound)/2;
if(UpperBound<LowerBound) return -1;
if(LowerBound == UpperBound && Array[MidPoint]>SearchElement) return MidPoint;
if(Array[MidPoint]>SearchElement) return Upper_Bound(LowerBound, MidPoint, SearchElement, Array);
if(Array[MidPoint]<=SearchElement) return Upper_Bound(MidPoint+1, UpperBound, SearchElement, Array);
}
int Lower_Bound(int LowerBound, int UpperBound, int SearchElement, int Array[]){
int MidPoint = (LowerBound + UpperBound)/2;
if(UpperBound<LowerBound) return -1;
if(LowerBound == UpperBound && Array[MidPoint]>=SearchElement) return MidPoint;
if(Array[MidPoint]>=SearchElement) return Lower_Bound(LowerBound, MidPoint, SearchElement, Array);
if(Array[MidPoint]<SearchElement) return Lower_Bound(MidPoint+1, UpperBound, SearchElement, Array);
}
int main(){
int Size;
cin>>Size;
int OriginalArray[Size];
for(int i = 0;i<Size;i++) cin>>OriginalArray[i];
}