Binary Search Program in C++ (For CBSE XII Students)
//Binary search using UDF
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
int BinarySearch(int [], int, int);
const int size=50;
void main()
{
int a[size], n, i, res=-1, s;
clrscr();
cout<<"Enter the size of array you want (Max. "<<size<<") : ";
cin>>n;
randomize();
for(i=0;i<n;i++)
{
cout<<"Enter the values in sorted order : ";
cin>>a[i];
}
char ch;
//Displaying the array.
cout<<"\nThe array elements are : \n";
for(i=0;i<n;i++)
cout<<setw(5)<<a[i];
cout<<endl;
do
{
cout<<"\nEnter the value you want to search in an array : ";
cin>>s;
res=BinarySearch(a,n,s);
if(res==-1)
cout<<"\nThe number "<<s<<" is not in an array\n";
else
cout<<"\nThe number "<<s<<" is found at index no. "<<res<<endl;
cout<<"\nDo you want to search again ? ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
int BinarySearch(int a[], int n, int s)
{
int beg, last, mid;
beg=0, last=n-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(a[mid]==s)
{
return mid;
}
else if(s>a[mid])
{
beg=mid+1;
}
else
{
last=mid-1;
}
}
return -1;
}
/*
Output:
Enter the size of array you want (Max. 50) : 5
Enter the values in sorted order : 12
Enter the values in sorted order : 23
Enter the values in sorted order : 45
Enter the values in sorted order : 67
Enter the values in sorted order : 89
The array elements are :
12 23 45 67 89
Enter the value you want to search in an array : 45
The number 45 is found at index no. 2
Do you want to search again ? y
Enter the value you want to search in an array : 48
The number 48 is not in an array
Do you want to search again ? n
*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
int BinarySearch(int [], int, int);
const int size=50;
void main()
{
int a[size], n, i, res=-1, s;
clrscr();
cout<<"Enter the size of array you want (Max. "<<size<<") : ";
cin>>n;
randomize();
for(i=0;i<n;i++)
{
cout<<"Enter the values in sorted order : ";
cin>>a[i];
}
char ch;
//Displaying the array.
cout<<"\nThe array elements are : \n";
for(i=0;i<n;i++)
cout<<setw(5)<<a[i];
cout<<endl;
do
{
cout<<"\nEnter the value you want to search in an array : ";
cin>>s;
res=BinarySearch(a,n,s);
if(res==-1)
cout<<"\nThe number "<<s<<" is not in an array\n";
else
cout<<"\nThe number "<<s<<" is found at index no. "<<res<<endl;
cout<<"\nDo you want to search again ? ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
int BinarySearch(int a[], int n, int s)
{
int beg, last, mid;
beg=0, last=n-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(a[mid]==s)
{
return mid;
}
else if(s>a[mid])
{
beg=mid+1;
}
else
{
last=mid-1;
}
}
return -1;
}
/*
Output:
Enter the size of array you want (Max. 50) : 5
Enter the values in sorted order : 12
Enter the values in sorted order : 23
Enter the values in sorted order : 45
Enter the values in sorted order : 67
Enter the values in sorted order : 89
The array elements are :
12 23 45 67 89
Enter the value you want to search in an array : 45
The number 45 is found at index no. 2
Do you want to search again ? y
Enter the value you want to search in an array : 48
The number 48 is not in an array
Do you want to search again ? n
*/
No comments
Post your comments