P. 1
binsearch.txt

binsearch.txt

|Views: 0|Likes:
Published by adddata
binary search
binary search

More info:

Published by: adddata on Mar 26, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

03/26/2013

pdf

text

original

#include"stdio.

h"
void sort(int a[10],int n);
void bina(int a[],int,int,int,int);
main()
{
int a[10],i,x,n,mid,l=0,h;
printf("Enter NO of Elements");
scanf("%d",&n);h=n-1;
mid=(h+l)/2;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("Elements are %d\n",a[i]);
printf("Enter an Element to Search");
scanf("%d",&x);
sort(a,n);
for(i=0;i<n;i++)
printf("Elements are %d\n",a[i]);
bina(a,l,x,mid,h);
// for(i=0;i<n;i++)
// printf("Elements are %d\n",a[i]);
}
void sort(int a[10],int n)
{
int i=0,j=0,temp;
for(;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;
}
}
void bina(int a[10],int l,int x,int mid,int h)
{
mid=(h+l)/2;
if(a[mid]==x)
{
printf("Element Found at %d position\n",mid);
exit(0);
}
else if(a[mid]<x)
{
h=mid;
bina(a,l,x,mid,h);
}
if(a[mid]>x)
{
l=mid+1;
bina(a,l,x,mid,h);
}
}
}

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->