Wednesday, February 8, 2012

C Program For Binary Search With Flowchart

Aim 
To write a c program to perform binary search

Program
#include<stdio.h>
main()
{system("clear");
int n,a[100],i,j,temp,s,low,high,mid,flag=0;
printf("Enter The limit");
scanf("%d",&n);
printf("Enter The number\n");
for(i=0;i<n;i++)
{scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("The sorted list of numers\n\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
printf("\nEnter The number to be searched");
scanf("%d",&s);
low=0;
high=n-1;
while(low<=high)
{mid=(low+high)/2;
if(s==a[mid])
{
printf("The numberis found at The position%d\n\n",mid+1);
flag++;
break;
}
else if(s>a[mid])
low=mid+1;
else
high=mid-1;
}
if(flag==0)
printf("The number is not found \n\n");
}

Flowchart




Tags : flow chart and algorithm for binary search in c,c program for binary search,binary search flow chart and algorithm free,program in c for binary search

No comments:

Post a Comment