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
When would you use a tuple?
We know that Arrays are objects so why cannot we write strArray.length()?
Reverse a linked list from the middle.
Why are b trees used?
How arraylist increase its size?
Define depth and height of a node?
Is quicksort a stable algorithm?
Which is better hashmap or hashtable?
Is hashtable fail fast?
How to do the intersection of two sorted arrays?
Can treemap have null values?
How to find 3rd element from end in a linked list in one pass?
Are the expressions arr and &arr same for an array of integers?
What is a property class?
Name some applications which use linked lists.