EmbLogic's Blog

Binary search for sorted arrays of 10 elements

#include<stdio.h>
#define SIZE 10
int main()
{
int i,arr[SIZE],key;
int l=0,h=SIZE-1,m;
printf(“Enter the numbers in array\n”);
for(i=0;i<SIZE;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“Enter the key”);
scanf(“%d”,&key);
while(l<=h)
{
m=(l+h)/2;
if(key==arr[m])
{
printf(“Element %d is found at %d position”,key,m);
break;
}
else if(key<arr[m])
{
h=m-1;
}
else if(key>arr[m])
{
l=m+1;
}
if(l>h)
{
printf(“not found”);
break;
}
}
return 0;
10,3-17       Top
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>