#include<iostream>
using namespace std;
int main(void)
{
//Declaration of variables
int array[10];
int i, j, N, temp, keynum;
int low, mid, high;
cout<<"\t\t@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
cout<<"\t\t@ @"<<endl;
cout<<"\t\t@ Binary @"<<endl;
cout<<"\t\t@ @"<<endl;
cout<<"\t\t@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
cout<<endl<<endl;
cout<< "Enter the value of N" << endl;
cin>> N;
cout<< "Enter the elements one by one" << endl;
for(i=0; i<N; i++)
{
cin>> array[i];
}
//Bubble sorting begin
for(i=0; i<N; i++)
{
for(j=0; j< (N-i-1); j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
cout<< "sorted array is..."<< endl;
for(i = 0; i < N; i++)
{
cout<< ""<<endl <<array[i]<<endl;
}
cout<< "Enter the element to be search"<<endl;
cin>> keynum;
//Binary searching begins
low = 1;
high = N;
do
{
mid = (low + high)/2;
if (keynum < array[mid])
high = mid - 1;
else
if(keynum > array[mid])
low = mid + 1;
} while (keynum != array[mid] && low <= high); //End of do-while loop
if (keynum == array[mid])
{
cout << "SUCCESSFUL SEARCH" <<endl;
}
else
{
cout<< "Search is FAILED " <<endl;
}
return 0;
}
J.W. Production
|