-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBINARY_SEARCHING.CPP
65 lines (61 loc) · 1.04 KB
/
BINARY_SEARCHING.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void binary(int arr[],int num1,int low,int high);
void binary(int arr[],int num1,int low,int high)
{
int mid=(low+high)/2;
if(num1==arr[mid])
{
cout<<"ELEMENT FOUND";
getch();
exit(0);
}
if(low>high)
{
cout<<"ELEMENT DOES NOT EXIT";
getch();
exit(0);
}
if(num1<arr[mid])
binary(arr,num1,low,mid-1);
if(num1>arr[mid])
binary(arr,num1,mid+1,high);
}
void main()
{
clrscr();
int arr[20],n,i,num,j;
cout<<"HOW MANY ELEMENTS DO YOU WANT TO INSERT....?"<<endl;
cin>>n;
cout<<"ENTER THE ELEMENTS :"<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
cout<<arr[i]<<"\t";
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
arr[i]=arr[i]+arr[j];
arr[j]=arr[i]-arr[j];
arr[i]=arr[i]-arr[j];
}
}
}
cout<<endl<<"THE SORTED ARRAY IS :"<<endl;
for(i=0;i<n;i++)
{
cout<<arr[i]<<"\t"<<endl;
}
cout<<"ENTER THE ELEMENTS DO YOU WANT TO SEARCH :"<<endl;
cin>>num;
binary(arr,num,0,n-1);
getch();
}