-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search.cpp
55 lines (52 loc) · 1.23 KB
/
binary_search.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
using namespace std;
// For increasing order
int binarySearch(int arr[],int size,int key){
int start = 0;
int end = size -1;
//smart step to avoid overflow of max integer value
int mid = start + (end-start)/2; // (start+end)/2;
while(start<=end){
if(key==arr[mid]){
return mid;
}
if(key<arr[mid]){
end = mid-1;
}else{
start = mid+1;
}
mid = start + (end-start)/2;
}
return -1;
}
// For decreasing order
int binarySearch2(int arr[],int size,int key){
int start = 0;
int end = size -1;
int mid = start + (end-start)/2;
while(start<=end){
if(key==arr[mid]){
return mid;
}
if(key<arr[mid]){
start = mid+1;
}else{
end = mid-1;
}
mid = start + (end-start)/2;
}
return -1;
}
int main() {
int incr[6] = {8,4,29,99,136,200};
int decre[7] = {170,111,97,71,55,40,10};
int incrIndex = binarySearch(incr,6,99);
cout<<incrIndex<<endl;
int decreIndex = binarySearch2(decre,7,130);
cout<<decreIndex<<endl;
return 0;
}
// Time complexity:
// worst case = O(log(n))
// best case = O(1)
// average case = O(log(n))