C++ Program to find input value using Binary search
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i,j,mid,no,found=0;
clrscr();
cout<<"Enter 10 values: \n";
for(i=0;i<10;i++)
cin>>arr[i];
cout<<"Enter value to find: ";
cin>>no;
i=0; j=9;
while(!found && i<=j)
{
mid=(i+j)/2;
if(arr[mid]==no)
found=1;
else
if(arr[mid]<no)
i=mid+1;
else
j=mid-1;
}
if(found)
cout<<"Value found at "<<mid<<" index";
else
cout<<"Value not found";
getch();
}
#include<conio.h>
void main()
{
int arr[10],i,j,mid,no,found=0;
clrscr();
cout<<"Enter 10 values: \n";
for(i=0;i<10;i++)
cin>>arr[i];
cout<<"Enter value to find: ";
cin>>no;
i=0; j=9;
while(!found && i<=j)
{
mid=(i+j)/2;
if(arr[mid]==no)
found=1;
else
if(arr[mid]<no)
i=mid+1;
else
j=mid-1;
}
if(found)
cout<<"Value found at "<<mid<<" index";
else
cout<<"Value not found";
getch();
}
No comments:
Post a Comment