Dec
28
2008

C++ Program Program for Binary Search.

C++ Program Program for Binary Search.

/**************************************************************	
	Author: Arun Vishnu M V
	Web: www.arunmvishnu.com
	Description: C++ Program Program for Binary Search.
***************************************************************/
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
	int ar[100],beg,mid,end,i,n,search;
	clrscr();
	cout<<"How many numbers in the array: ";
	cin>>n;
	cout<<"Enter "<<n<<" numbers in ascending order --> ";
	for(i=0;i<n;i++)
		cin>>ar[i];
	beg=0;
	end=n-1;
	cout<<"Enter a number to search: ";
	cin>>search;
	while(beg<=end)
	{
		mid=(beg+end)/2;
		if(ar[mid]==search)
		{
			cout<<"\nItem found at position "<<(mid+1);
			getch();
			exit(0);
		}
		if(search>ar[mid])
			beg=mid+1;
		else
			end=mid-1;
	}
	cout<<"\nSorry! "<<search<<" doesnot found.";
	getch();
}

Related Posts

Author: Arun Vishnu

8 Comments + Add Comment

  • How to get that program?

  • thanx.
    its good.

  • its good…but pls provide it with algorithm n output also…or mail them on my id…pls

  • since i hv seen many programs on net but it doesn’t works…
    but this is the best of all its really works n easy to understand

  • It is good…but…quite difficult….

  • thank youuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

  • programming is much difficult…………

  • thnku….done it in a simple way….wl easy 2 understand….

Leave a comment

Archives

Highslide for Wordpress Plugin