Posted by MindBreaker at 2:03 AM
Read our previous post
Write a program which uses
Binary Search to search the elements 45,and 78 and delete it.
NUMBERS=[11,22,33,36,45,52,57,60,64,78]
Source Code;
// Delete
in Array for number Binary Deletion
#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;
}
////////////////////////////////////////////////////////
int
*arry(int arr[10],int
s)
{
int
elements ;
int
position ;
for (
elements = 0 ; elements < 10 ; elements++ )
{
if((arr[elements]==45) || (arr[elements]==78))
{
arr[elements]=NULL;
}
else
{
}
}
for(elements;elements<9;elements++)
{
arr[elements]=arr[elements+1];
}
return
arr;
}
};
void
main()
{
del dl;
int
arr[10]={11,22,33,36,45,52,57,60,64,78}, position, elements,*ptr,searchb,serch;
cout<<"Enter
number to search : "<<endl;
cin>>serch;
searchb=dl.bserach(arr,serch);
if(searchb==1)
{
cout<<"Element found : ";
ptr=dl.arry(arr,serch);
}
else if(searchb==0)
{
cout<<"Element not found ! ";
}
else
cout<<"Not found";
cout<<"Resultant
is : "<<endl;
for(elements=0;elements<10-1;elements++)
{cout<<ptr[elements]<<endl;}
}
Output:
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.