forked from Harry-003/Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-search.cpp
More file actions
45 lines (30 loc) · 753 Bytes
/
binary-search.cpp
File metadata and controls
45 lines (30 loc) · 753 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
43
44
45
#include<iostream>
using namespace std;
int binarySearch(int arr[], int size, int key){
int start = 0;
int end = size-1;
int mid = start + (end-start)/2;
while(start <= end){
if (arr[mid]==key){
return mid;
}
//go to start wala part
if (key > arr[mid]){
start = mid+1;
}
else{
end = mid - 1;
}
mid = start + (end-start)/2;
}
return -1;
}
int main(){
int even[8] = {2,4,6,8,12,18,19,58};
int odd[5] = {3,4,5,6,7};
int index = binarySearch(even, 6, 8);
cout<<"Index of 8 is "<<index <<endl;
int oddIndex = binarySearch(odd, 5, 5);
cout<<"Index of 5 is "<<oddIndex<<endl;
return 0;
}