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
List some applications of tree-data structure?
How do we find duplicate elements in an array?
Write an algorithm to check if there is a loop in a doubly linked list.
In which matrix, we can use multilinked structures?
What are data structures in programming?
Define left-in threaded tree?
Calculate the efficiency of sequential search?
If you have to store one lakh objects, what will be a better option- a hash map or an array list?
What is the purpose of sorting?
Can arraylist be null?
Is there any difference between int[] a and int a[]?
Tell me can the size of operator be used to tell the size of an array passed to a function?
List some applications of multilinked structures?
How do you sort elements in an arraylist?
What do you mean by complexity of search algorithm?