-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search.cpp
38 lines (37 loc) · 1.36 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
#include<iostream>
using namespace std;
void binary_search(int[],int,int);
int main()
{
int arr[100],i,j,arr_length,n;
cin>>arr_length; //input array length less than 100
for(i=0;i<arr_length;i++) cin>>arr[i];
for(i=1;i<arr_length;i++) //sorting array
{
int temp=arr[i];
for(j=i-1;j>=0&&arr[j]>temp;j--)
{
arr[j+1]=arr[j];
arr[j]=temp;
}
}
cin>>n;
binary_search(arr,arr_length,n);
}
void binary_search(int x[],int length,int num)
{
int mid,end,start=0,parity=0;
for(end=length-1;start<=end;)
{
mid=(start+end)/2;
if(x[mid]<num) start=mid+1;
else if(x[mid]>num) end=mid-1;
else if(x[mid]==num)
{
cout<<num<<" found at index "<<mid+1<<endl;
parity++;
break;
}
}
if(parity==0) cout<<num<<" not found in array"<<endl;
}