Posted by MindBreaker at 1:59 AM
Read our previous post
Translate binary search and
insertion algorithm into a program which finds either the location LOC where
ITEM appears in ARRAY or the location
Source Code;
/////
Binary Searching
#include <iostream>
using namespace std;
class del
{
public:
int bserach(int arr[10],int
searchval)
{
int
Lbound=0,ubound=10-1,mid;
while(Lbound<=ubound)
{
mid=ubound+Lbound/2;
if(searchval>arr[mid])
{
Lbound=mid+1;
}
else
if(searchval<arr[mid])
{
Lbound=mid-1;
}
else
return 1;
}
return 0;
}
////////////////////////////////////////////////////////
};
void
main()
{
del dl;
int
arr[10]={11,22,33,36,45,52,57,60,64,78}, searchb,serch, number;
cout<<"Number
";
cin>>number;
cout<<"
Search Number is : "<<number<<endl;
searchb=dl.bserach(arr,number);
if(searchb==0)
{
cout<<"Element
found : ";
}
else if(searchb==1)
{
cout<<"Element not
found ! ";
}
}
Output:
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.