forked from Andyy-18/CPP-BASICS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.cpp
More file actions
42 lines (32 loc) · 804 Bytes
/
BinarySearch.cpp
File metadata and controls
42 lines (32 loc) · 804 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int binsearch(int arr[], int size, int key){
int start = 0;
int end = size-1;
// int mid = (start+end)/2;
int mid = start + (end - start)/2;
while(start<=end){
if(arr[mid] == key){
return mid;
}
if(arr[mid]<key){
start=mid+1;
}
else{
end=mid-1;
}
// mid = (start+end)/2;
mid = start + (end - start)/2;
}
return -1;
}
int main()
{
int even[6] = {2,4,6,8,12,18};
int odd[5] = {3,8,11,14,16};
int index1 = binsearch(even,6,12);
int index2 = binsearch(odd,5,16);
cout<<"Index of 12 is : "<<index1<<endl;
cout<<"Index of 16 is : "<<index2<<endl;
return 0;
}