Answer Posted / arnoldindia
#include<conio.h>
#include<iostream.h>
#include<process.h>
int binarysearch(int list[], int end, int target, int &locn)
{
int first=0, mid, last=end;
while(first<=last)
{
mid=(first+last)/2;
if(target>list[mid])
first=mid+1;
else if(target<list[mid])
last=mid-1;
else
break;
}
locn=mid+1;
return(target==list[mid]);
}
void main()
{
int a[10],i,s=0,n,loc,flag=0;
clrscr();
cout<<"\n Enter the no. of element to store:\n";
cin>>n;
cout<<"Enter the Elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n The Elements are:\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
cout<<"\n Enter the Element to search:\n";
cin>>s;
if(binarysearch(a,n,s,&loc))
cout<<"\nThe element "<<s<< " is available at location
"<<loc<<endl;
else
cout<<"\nThe element "<<s<< " is not found in the List"<<endl;
}
| Is This Answer Correct ? | 51 Yes | 23 No |
Post New Answer View All Answers
Define linear data structures?
Can you please explain the difference between string and an array?
Can you list out the areas in which data structures are applied extensively?
Can treemap key null?
What are types of Collision Resolution Techniques and the methods used in each of the types?
How to find the missing element in integer array of 1 to 7?
What is the order of selection sort?
What is the use of treemap?
What is a stable sort?
Does arraylist guarantee insertion order?
What does enum do?
Is binary tree a binary search tree?
Why it is important to have aligned addresses? What is the exception generated when there is a misaligned address?
How many types of search algorithms are there?
What is the advantage of the heap over a stack?