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

Is pointer a variable in data structure?

520


What are the standard ways in which a graph can be traversed?

482


What do you mean by Logical Error

553


Why do we use binary search?

475


There are 2 int type array data type. One is containing 50 elements, and another one is containing 30 elements. Can we assign the array of 50 elements to an array of 30 elements?

837






What is scalar example?

517


Can treemap key null?

585


What is a string or array type?

454


What are sorting algorithms used for?

529


What do you mean by selection sort?

484


Explain about set and their types in a collection?

472


What are the difference between arraylist and linkedlist from the perspective of sorting?

548


Define primary data structures?

577


Does treeset remove duplicates?

500


Write the importance of hashing?

599