Back To C++ Basic Index
binary search is faster than linear search but to implement binary search array must be sorted
//program to demonstrate binary search on a single dimensional array (to implement binary search array should be sorted #include
#include
void main() { int arr[10]={10,20,30,40,50,60,70,80,90,100}; int beg,last,mid; int found=0; beg=0; last=9; int element; clrscr(); cout<<"\nenter element to search for "; cin>>element; while(beg<=last) { mid=(beg+last)/2; if(element==arr[mid]) { cout<<"\nelement found at "<
arr[mid]) { beg=mid+1; } else { last=mid-1; } } if(found==0) { cout<<"\nelement not found"; } getch(); }