Answer Posted / rahul awasthi
Answer
# 1 #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 ? | 21 Yes | 20 No |
Post New Answer View All Answers
What is an ordered map?
Mention the data structures which are used in graph implementation.
Give the example of validating the parenthesis of expression using stack.
What is bubble sort?
Which is the parent class of sortedset
What are the disadvantages of linked list over array?
Why set will not allow duplicates?
Name few concurrent collection classes?
Is linked list faster than array?
What does it mean to sort an array?
How do you find the second largest element in an array of integers?
How efficient is binary search?
Name some applications which use linked lists.
Why null is allowed in hashmap?
How to sequentially represent max-heap?