-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
28 lines (24 loc) · 841 Bytes
/
binarySearch.cpp
File metadata and controls
28 lines (24 loc) · 841 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
@Author: Sai Sagar Jinka
#include <iostream>
using namespace std;
int binarySearch(int arr[], int first, int last, int x){
if(last > 1)
{
int mid = first + (last - 1)/2;
if(arr[mid] == x)
return mid;
if(arr[mid] > x)
return binarySearch(arr, first, mid - 1, x);
return binarySearch(arr, mid + 1, last, x);
return -1;
}
}
int main(){
int array[] = {1,3,4,5,7,8,9,90};
int n = sizeof(array)/sizeof(array[0]);
int index = binarySearch(array,0,n-1,5);
// cout << "The size of array is " << sizeof(array);
// cout << "The size of one element is " << sizeof(array[0]);
cout<<"The index of searched element is " << index << endl;
return 0;
}