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