binary search program in c language


Posted Date:     Total Responses: 0    Posted By: sarathveera   Member Level: Gold   Points/Cash: 3   


#include
#include

void main()
{
int array[10];
int i, j, N, temp, keynum;
int low,mid,high;

clrscr();

printf("Enter the value of N\n");
scanf("%d",&N);

printf("Enter the elements one by one\n");
for(i=0; i {
scanf("%d",&array[i]);
}
printf("Input array elements\n");
for(i=0; i {
printf("%d\n",array[i]);
}
/* Bubble sorting begins */
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;
}
}
}
printf("Sorted array is...\n");
for(i=0; i {
printf("%d\n",array[i]);
}

printf("Enter the element to be searched\n");
scanf("%d", &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 */

if( keynum == array[mid] )
{
printf("SUCCESSFUL SEARCH\n");
}
else
{
printf("Search is FAILED\n");
}

} /* End of main*/


Project Feedbacks

Author: Member Level: BronzeRevenue Score: 5 out of 55 out of 55 out of 55 out of 55 out of 5
can this program be considered as example for binary search

#include
#include
main()
{
int n,z,z1,i,t,j,mem[500],find,temp;
float x,k;
clrscr();
printf("\n Enter no of terms :");
scanf("%d",&n);
k=n;
printf("\n Enter terms \n\t");
for(i=1;i<=n;i++)
scanf(" %d",&mem[i]);
for(i=1;i<=n;i++)
{
x=(i/k)*100;
printf("\n %.0f'%' sorted...",x);
for(j=1;j<=i;j++)

if(mem[j]>mem[i])
{
temp=mem[j];
mem[j]=mem[i];
mem[i]=temp;
}

}
printf("\n\n\ Sorted List :-\n\n\t");
for(i=1;i<=n;i++)
printf(" %d",mem[i]);
n=n+1;


printf("\n Enter a term to search :");
scanf("%d",&find);
k=0;
z=n;

while(z>0)
{
if(find == mem[z/2])
{
k++;
break;
}
else if(find < mem[z/2] && find >= mem[1])
{
t=z/2;
if( (t%2) != 0 )
{
z=t+1;
}
else
z=z/2;

}

else if(find > mem[z/2] && find <= mem[n-1] )
{

for(i=(z/2), j=1 ; i < n-1 ; i++ ,j++)
mem[j]= mem[i+1];
z--;
}
else
break;
}

if(k==0)
{ printf("\n not found");
}
else
printf("\n found");
getch();
}



Author: Member Level: BronzeRevenue Score: 5 out of 55 out of 55 out of 55 out of 55 out of 5
can this program be considered as example for binary search

#include
#include
main()
{
int n,z,z1,i,t,j,mem[500],find,temp;
float x,k;
clrscr();
printf("\n Enter no of terms :");
scanf("%d",&n);
k=n;
printf("\n Enter terms \n\t");
for(i=1;i<=n;i++)
scanf(" %d",&mem[i]);
for(i=1;i<=n;i++)
{
x=(i/k)*100;
printf("\n %.0f'%' sorted...",x);
for(j=1;j<=i;j++)

if(mem[j]>mem[i])
{
temp=mem[j];
mem[j]=mem[i];
mem[i]=temp;
}

}
printf("\n\n\ Sorted List :-\n\n\t");
for(i=1;i<=n;i++)
printf(" %d",mem[i]);
n=n+1;


printf("\n Enter a term to search :");
scanf("%d",&find);
k=0;
z=n;
printf("\n n=%d",z);

while(z>0)
{
if(find==mem[z/2])
{
k++;
break;
}
else if(find=mem[1])
{
t=z/2;
if( (t%2) != 0 )
{
z=t+1;
}
else
z=z/2;

}

else if(find>mem[z/2] && find<=mem[n-1] )
{

for(i=(z/2),j=1;i mem[j]=mem[i+1];
z--;
}
else
break;
}

if(k==0)
{ printf("\n not found");
}
else
printf("\n found");
getch();
}



Author: Member Level: BronzeRevenue Score: 2 out of 52 out of 5
If we enter only one number above program fails.

so to correct it we have to assign low=0 instead of low=1;


Post Feedback
You must Sign In to post a feedback.
Next Project: hospital management
Previous Project: SUDOKU Game Code

Return to Project Index

Post New Project


Related Projects

Awards & Gifts
Active Members
TodayLast 7 Daysmore...

ISC Technologies, Kochi - India. Copyright © All Rights Reserved.