Write a program for Binary sort & search.
#include <stdio.h>
#include <conio.h>
void main ( )
{
int nos[5], i, j, tem, no, b = 0, l = 4, m, flag = 1;
clrscr ( );
for ( i = 0; i < 5; i++ )
{
printf ("\n\t\t\t Enter a number...........:"); scanf ("%d", &nos[i]);
}
printf ("\n\n\n\t\t\t Enter a number for searching...........:"); scanf ("%d", &no);
for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 5 - ( i + 1 ); j++ )
{
if (nos[ j] >= nos[ j + 1 ] )
{
tem = nos[ j];
nos[ j] = nos[ j + 1 ];
nos[ j + 1 ] = tem;
}
}
}
for ( i = 0; i < 5; i++ )
printf ("\n\t\t Position : - %d \t Output Number : - %d", i, nos[i]);
while ( b <= l )
{
m = ( b + l ) / 2;
if ( nos[m] == no )
{
flag = 0;
break;
}
if ( nos[m] < no )
{
b = m + 1;
}
else
{
l = m - 1;
}
}
if ( flag == 0 )
printf ("\n\n\n\n\t\t\t\t SUCCESFUL");
else
printf ("\n\n\n\n\t\t\t\t UNSUCCESFUL");
getch ( );
}
Click here to contact us
No comments:
Post a Comment