Write a Binary Search program

Answer Posted / saisharath

#include<stdio.h>
#include<conio.h>
int nr_bin_search(int[],int,int);
void main()
{
int key,i,n,index,l[20];
printf("\n enter the number of elements in the list:");
scanf("%d",n);
printf("\n enter the elements of the list:");
for(i=0;i<n;i++)
scanf("%d",&l[i]);
printf("\n enter the key element to be searched in the
list:");
scanf("%d",&key);
index=nr_bin_search(l,n,key);
if(index==-1)
printf("\n search completed,element%d found in the list at
position %d",key,index);
getch();
}
int nr_bin_search(ints[],int n,int e)
{
int low_val,mid_val,high_val;
low_val=0;
high_val=0;
while(high_val>=low_val)
{
mid_val=(low_val+high_val)/2;
if(s[mid_val]==e)
return(mid_val);
if(s[mid_val]<e)
low_val=mid_val+1;
else
high_val=mid_val-1;
}
return-1;
}

Is This Answer Correct ?    11 Yes 13 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is reduction to sorting method?

606


Why use a tuple instead of a list?

543


Explain the common uses of threaded binary tree.

677


Define splay tree?

668


How null key is handled in hashmap?

541






What things you would care about to improve the performance of application if its identified that its db communication that needs to be improved?

610


Why quicksort is better than merge sort?

562


If you are using c language to implement the heterogeneous linked list, explain what pointer type will you use?

692


What is dynamic array in excel?

528


Describe tree rotation in avl tree.

654


How to reverse a singly linked list?

627


What are the different types of data structures?

608


State the merits of linear representation of binary trees?

674


What is the space complexity of quicksort?

573


What is bubble insertion selection sort?

561