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

Which is the fastest sorting?

628


Explain the types of linked lists.

560


What is data structures in computer science?

491


Explain the Queue

583


What is binary tree example?

570






What is the meaning of arraylist?

496


What are binary trees?

587


How do you declare A pointer to array of three chars

534


What is a hash index?

476


How does the bubble sort work?

502


What is collection process?

516


Find duplicates in infinite range. Which data structure to be used to give efficient solution?

539


Define balancing condition for AVL Tree.

553


Write the stack overflow condition.

594


Is collection a class or interface?

507