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
What are the tasks performed during inorder traversal?
What is the use of bubble sort?
How do I push an element into a stack?
On clicking a node in a tree, all the adjacent edges are turned on. Calculate min of clicks such that all the edges are turned on.
Can arraylist hold duplicates?
Can arraylist store primitives?
Write a Program for Linked list manipulation.
What is the difference between hashset and linkedhashset?
Why do we use sorting?
What is bubble sort used for?
Does hashmap allow duplicate keys?
Define an algorithm. What are the properties of an algorithm?
What is the Difference between treemap and hashmap?
List the area of applications where stack data structure can be used?
Describe the complexity of Quick Sort