-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
48 lines (37 loc) · 1.18 KB
/
binarySearch.cpp
File metadata and controls
48 lines (37 loc) · 1.18 KB
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
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int n, int val)
{
int s=0 ,e = n-1;
while(s<=e)
{
int mid = s + (e-s)/ 2; // middle point = starting position + Half of current search space
if(arr[mid]==val)
return mid; // return it if you found it in the middle
// not found ? will move rigth or left depend on its value ( Sorted Array :) )
else if(arr[mid]<val)
s=mid+1; // get rid of the left half : new starting position = after the previous middle
else if(arr[mid>val])
e = mid-1; // get rid of the right half : new end position = before the previous middle
}
return -1;
}
int main()
{
int n;
cout<<"Enter the number of elements in the array"<<endl;
cin>>n;
int* arr = new int[n] ;
for(int i=0 ;i<n ;i++)
cin>>arr[i];
sort(arr, arr+n);
int val;
cout<<"Enter the value to be searched in the array"<<endl;
cin>>val;
int res = binarySearch(arr,n,val);
if(res==-1)
cout<<"Element does not exist in the array"<<endl;
else
cout<<"Element "<<val<<" found at index "<<res<<" in the array"<<endl;
delete arr;
}