Sunday, June 10, 2012

CS 110 COMPUTER PROGRAMMING LAB - Basic C Programs




//Program To Execute Binary Search.

#include<stdio.h>
#include<conio.h>

void main()
{
int ar[50],i,j,beg=0,mid,end,num,temp,item;
clrscr();
printf("\nEnter the number of elements of the Array:\t");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\nEnter element %d :",i+1);
scanf("%d",&ar[i]);
}
for(i=0;i<num;i++)
for (j=0;j<num-i-1;j++)
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
printf("\nEnter the element to be searched:");
scanf("%d",&item);
end=num-1;
mid=(beg+end)/2;
while(beg<=end && item!=ar[mid])
{
if(item>ar[mid])
{
beg=mid+1;
}
else
{
end=mid-1;
}
mid=(beg+end)/2;
}
if(item==ar[mid])
printf("\nThe Item is found");
else
printf("\nThe item is not found");
getch();
}





0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More