Tuesday, January 13, 2015

C++ Program for Linear and Binary Search

#include<iostream>
#include<iomanip>
using namespace std;

bool linear_search_flag(double [],int);
void result(bool);
int linear_search(double [],int);
void Result(int);
void more_positions(double [],int );
void binary_search_flag(double [],int );
void binary_search(double [],int );

int main()
{
const int SIZE = 18;
bool flag;
double Numbers[SIZE]={5658845, 8080152, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
flag=linear_search_flag(Numbers,SIZE);
result(flag);

int position;
position=linear_search(Numbers,SIZE);
Result(position);
more_positions(Numbers,SIZE );
binary_search_flag(Numbers,SIZE);
binary_search(Numbers,SIZE);

return 0;
}

bool linear_search_flag(double Array[],int size )
{
double Num;
bool flag=0;
cout << "Enter the number: ";
cin >> Num;
for( int i=0 ; i<size ; i++ ) //OR for( int i=0 ; i<size&&flag==0 ; i++ )
{
if( Num==Array[i] )
{
flag=1;
break;
}
}
return flag;
}

void result(bool flag)
{
if( flag )
cout << "\nThe number exists in the list.\n";
else
cout << "\nThe number does NOT exist int the list.\n";
}

int linear_search(double Array[],int size )
{
double Num;
int position=-1;
cout << "\n\nEnter the number: ";
cin >> Num;
for( int i=0 ; i<size ; i++ )
{
if( Num==Array[i] )
{
position=i;
break;
}
}
return position;
}

void Result(int position)
{
if( position==-1 )
cout << "\nThe number does NOT exist.\n";
else
cout << "\nThe number exists at position "
<< (position+1)
<< ".\n"
<< "And its subscript number in the array is "
<< position
<< endl;
}

void more_positions(double Array[],int size )
{
double Num;
bool position[18]={0};
cout << "\n\nEnter the number: ";
cin >> Num;
for( int i=0 ; i<size ; i++ ) //OR for( int i=0 ; i<size&&flag==0 ; i++ )
{
if( Num==Array[i] )
{
position[i]=1;
}
}
bool exist=0;
for( int i=0 ; i<size ; i++ )
{
if(position[i])
{
exist=1;
break;
}
}
if(exist)
{
cout << setprecision(2) << fixed;
cout << "\nThe number "
<< Num
<< " exists at position(s)";
for( int i=0 ; i<size ; i++ )
{
if(position[i])
{
cout << (i+1)
<< "  ";
}
}
cout << ".\n";
cout << "And its subscript number is ";
for( int i=0 ; i<size ; i++ )
{
if(position[i])
{
cout << i
<< "  ";
}
}
cout << ".\n";
}
else
cout << "\nThe number "
<< Num
<< " does NOT exist.\n";
}

void binary_search_flag(double Numbers[],int SIZE )
{
int first,last,mid;
double Num;
bool flg=0;
cout << "\n\nEnter the number: ";
cin >> Num;
//mandatory...Asecnding or Descending and then accordingly
//here Ascending
for( int i=0 ; i<SIZE ; i++ )
{
double temp;
for( int j=i+1 ; j<SIZE ; j++ )
{
if( Numbers[j]<Numbers[i] )
{
temp=Numbers[i];
Numbers[i]=Numbers[j];
Numbers[j]=temp;
}
}
}
cout << setprecision(2) << fixed;
cout << "\nNow the numbers are:\n";
for( int i=0 ; i<SIZE ; i++ )
cout << Numbers[i]
<<"\t";

first=0;
last=SIZE-1;
while( first<=last && !flg )
{
mid=(first+last)/2;
if( Num==Numbers[mid] )
flg=1;
else if( Num>Numbers[mid] )
first = mid+1;
else
last = mid-1;
}
if( flg )
cout << "\nThe number exists.\n";
else
cout << "\nThe number does NOT exist.\n";
}

void binary_search(double Numbers[],int SIZE )
{
int first,last,mid;
double Num;
int position=-1;
cout << "\n\nEnter the number: ";
cin >> Num;
//mandatory...Asecnding or Descending and then accordingly
//here Descending
for( int i=0 ; i<SIZE ; i++ )
{
double temp;
for( int j=i+1 ; j<SIZE ; j++ )
{
if( Numbers[j]>Numbers[i] )
{
temp=Numbers[i];
Numbers[i]=Numbers[j];
Numbers[j]=temp;
}
}
}
cout << setprecision(2) << fixed;
cout << "\nNow the numbers are:\n";
for( int i=0 ; i<SIZE ; i++ )
cout << Numbers[i]
<<"\t";

first=0;
last=SIZE-1;
while( first<=last && position==-1 )
{
mid=(first+last)/2;
if( Num==Numbers[mid] )
position=mid;
else if( Num<Numbers[mid] )
first = mid+1;
else
last = mid-1;
}
if( position==-1 )
cout << "\nThe number does NOT exist.\n";
else
cout << "\nThe number Exists at position "
<< (position+1)
<< ".\n"
<< "And at its subscript value in the Array is "
<< position
<< ".\n";
}

No comments:

Post a Comment