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 do you understand by stable sort?

692


What are the types of linked list?

680


Which sorting algorithm is the slowest?

633


Traverse the given tree using Inorder, Preorder and Postorder traversals. Inorder : D H B E A F C I G J Preorder: A B D H E C F G I J Postorder: H D E B F I J G C A

853


Are hash tables ordered?

647


How can I learn data structures?

715


Define an equivalence relation?

817


Which is better hashmap or hashtable?

657


How do you find the depth of a binary tree?

738


How does linkedhashset work internally?

667


Why heap sort is not used?

661


Can we null keys in treemap?

755


What does the dummy header in the linked list contain?

933


Is boolean a data type?

663


What is time complexity of arrays sort?

691