Posts

Showing posts from March, 2019

Binary Search

Program on Binary Search #include<iostream.h> #include<conio.h> void main() {       clrscr(); int n, i, a[30], x, first, last, middle; cout<<"How many elements would you like to enter?:"; cin>>n; for (i=0; i<n; i++) { cout<<"Enter number "<<(i+1)<<": "; cin>>a[i]; } cout<<"Enter the number that you want to search:"; cin>>x; first = 0; last = n-1; middle = (first+last)/2; while (first <= last) {    if(a[middle] < x)    { first = middle + 1;    }    else if(a[middle] == x)    { cout<<x<<" is found in the array at the location "<<middle+1<<"\n"; break;    }    else { last = middle - 1;    }    middle = (first + last)/2; } if(first > last) {    cout<<x<<" is not found in the ...