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();
}

You may also like...

8 Responses

  1. Arjohn Lopez says:

    How to get that program?

  2. sanjeev says:

    thanx.
    its good.

  3. prateek says:

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

  4. salman says:

    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

  5. suraj says:

    It is good…but…quite difficult….

  6. mahdi says:

    thank youuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

  7. programming is much difficult…………

  8. Anjali Sivan says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *