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 a stable sort?
What is the basic of data structure?
What is sorting and its types?
Define circular list?
What is collections singletonmap?
Explain the uses of binary tree.
Can a hashset contain duplicates?
What exactly is a thread?
How do we search a specific element in an array?
Why do we study data structures?
Which is the parent class of abstractqueue class?
How do you check if a stack is empty or not?
Why would you use a linked list?
Can we store a string and integer together in an array?
What is application of data structure?