Write a Binary Search program

Answer Posted / s.devudu

#include<stdio.h>
int binarysearchr(int a[],int,int,int);
void main()
{
int a[],i,j,b,n,key,temp;
clrscr();
printf("enter the n value");
scanf("%d",&n);
printf("enter the array values");
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
for(i=0;i<=n;i++)
{
for(j=0;j<=n-1;j++)
{
if(a[j]>=a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("in an array after sorting");
scanf(%d\t",&a[i]);
printf("enter the key value");
for(i=0;i<=n;i++)
scanf(%d",&key);
b=binarysearchr(int a[],int i,int n,int key);
if(b)
printf("location of key element is %d\n",b);
else
printf("key not found");
getch();
}
int binarysearchr(int a[],int low,int high,int key);
{
int mid;
if(low>high)
return 0;
mid=(low+high)/2;
if(key==a[mid])
return mid;
else if(key<=a[mid])
return binarysearchr(a,mid-1,high,key);
else
reurn binarysearch(a,low,mid+1,key);
}

Is This Answer Correct ?    4 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a treemap chart?

533


Why is it important to sort waste?

552


What is tree and its properties?

590


Explain what are the major data structures used in the hierarchical data model?

606


How do you sort an array by value?

551






Define heap order property?

623


Is heap sort faster than quicksort?

547


Explain the common uses of threaded binary tree.

677


Is hashmap an object?

585


Why is quicksort not stable?

548


What does adt stands for?

558


What is the best complexity of bubble sort?

537


What is the difference between data type and data structure?

539


What is data type and its types?

613


Explain about the different lists available in the collection?

541