//Binary Search
//BCA Support
//www.bcasupport.xyz
#include <iostream.h>
#include <conio.h>
int num[10],a,b,temp,search;
void binary(int [],int,int);
void main()
{
clrscr();
cout<<"\nEnter 10 numbers :\n";
for(a=0;a<10;a++)
cin>>num[a];
for(a=0;a<9;a++)
for(b=a+1;b<10;b++)
{
if(num[a]>num[b])
{
temp=num[a];
num[a]=num[b];
num[b]=temp;
}
}
clrscr();
cout<<"\nNumbers list :\n";
for(a=0;a<10;a++)
cout<<num[a]<<endl;
cout<<"\nEnter number to search : ";
cin>>search;
binary(num,b,search);
getch();
}
void binary(int no[],int last,int search)
{
int mid=0,first=0,b;
while(1)
{
mid=(first+last)/2;
if(no[mid]==search)
{
cout<<endl<<search<<" found on position "<<mid+1;
break;
}
else if(search>no[mid])
first=mid+1;
else
last=mid-1;
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.