Pages

Wednesday, October 7, 2009

BINARY SEARCH PROGRAM(Under DataStructureSection)

#include
#include
typedef int ElementType;
#define NotFound (-1)
int BinarySearch(const ElementType A[ ], ElementType X, int N )
{
int Low, Mid, High;
Low = 0;
High = N - 1;
while( Low <= High )
{
Mid = ( Low + High ) / 2;
if( A[ Mid ] < X )
Low = Mid + 1;
else
if( A[ Mid ] > X )
High = Mid - 1;
else
return Mid;
}
return NotFound;
}
void main( )
{
    static int A[ ] = { 1, 3, 5, 7, 9, 13, 15 };
    const int SizeofA = sizeof( A ) / sizeof( A[ 0 ] );
    int i;
    clrscr();
    for( i = 0; i < 20; i++ )
    {
    printf( "BinarySearch of %d returns %d\n",i, BinarySearch( A, i, SizeofA ) );
    }
    getch();
}

No comments:

Post a Comment