My Profile

I, Das ShrikKrishna J. MCA III IMSCD&R, Ahmednagar.
Showing posts with label Binary Search (Iterative). Show all posts
Showing posts with label Binary Search (Iterative). Show all posts

Monday, 15 August 2011

Search an Element Binary Search (Iterative)

Write a Program in C to Search an Element in the Array using Binary Search (Iterative)
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],start,end,mid,i,num,flag=0;
clrscr();
printf("Enter 5 Numbers\n");
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched : ");
scanf("%d",&num);
start=0;
end=4;
mid=(start+end)/2;
while(start<=end)
{
if(a[mid]==num)
{
printf("Number (%d) found at position (%d)\n",num,mid+1);
flag=1;
break;
}
if(num>a[mid])
{
start=mid+1;
}
else
{
end=mid-1;
}
mid=(start+end)/2;
}
if(flag==0)
{
printf("\nDidnot find the number (%d)\n",num);
}
getch();
}

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls