Saturday, April 21, 2012

BINARY SEARCH using CLASS


Algorithm for BINARY SEARCH using CLASS
Step 1- START
Step 2- Creat an object for the class BINSRCH
Step 3- Read how many elements to be processed….ie(no)
Step 4- Call the Function “GETARR” with passing the “ no “ As parmeter through obj.
Step 5- Call the function “SORT” for sorting the element through obj
Step 6- DISPLAY the element Array  & RETURN
Step 7- Do the followings…..
a)      Display  “enter your Element to be search”
b)      Read the element
c)       Call the Member function “srch” passing the element as parameter
d)      ASK user for next run,  & Read the choice from USER( Y / N)
UNTILL the choice is other than “Y”
Step 8-STOP
------------------------------------------------------------------------------------------------------------
                                   FLOW CHART
-----------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
                                C++ IMPLEMENTATION
----------------------------------------------------------------------------------------------------------
//sample program for BINARY SEARCH IN AN ARRAY

#include<iostream.h>
#include<conio.h>
class bin
{
int arr[10],i,j,no,l,h,m;
public:
void getarr();
void output();
void sort();
void srch(int);
};
//----------DEFINATION FOR getarr()--------------------
void bin::getarr()
{
cout<<"\n\t\t HOW MANY ELEMENTS TO BE PROCESSED...?\n";
cin>>no;
cout<<"\n\t\t ENTER YOUR ELMENTS:...\n";
for(i=1;i<=no;i++)
cin>>arr[i];
cout<<"\n\t\t YOUR ENTERED ELEMENTS ARE:\n\t\t";
for(j=1;j<=no;j++)
{
cout<<"\t"<<arr[j];
}
return;
}

//-----------DEFINATION for-SORTING------------
void bin::sort()
{
for(i=1;i<=no-1;i++)
{
for(j=i+1;j<=no;j++)
{
if(arr[i]>arr[j])
{
int t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
cout<<"\n\t\t NOW YOUR SORTED ARRAY IS:...\n\t\t";
for(i=1;i<=no;i++)
{
cout<<"\t"<<arr[i];
}
return;
}
//--------------DEFINATION for -SEARCHING-------------
void bin::srch(int x)
{

l=1;
h=no;
while(l<=h)

{
m=(l+h)/2;
if(x==arr[m])
{
cout<<"\n\t\t:)\n\t\t ELEMENT IS FOUND\n\t\t ELEMENT LOCATION IS:\t"<<m;
return;
}
if(x>arr[m])
l=m+1;
else
h=m-1;
}
cout<<"\n\t\t:(\n\t\t SORRY...!ELEMENT "<<x<<"  IS NOT FOUND...\n\t\tPLEASE TRY AGAIN... \n";


}

//--------------------------MAIN FUNCTION-----------------------
void main()
{
clrscr();
bin b;
int x;
char ch;
cout<<"\n\t\t\tBINARY SEARCH\n\t\t~~~~~~~~~~~~~~~~~~~~~~~\n";
b.getarr();
b.sort();
do
{
cout<<"\n\n ENTER YOR ELEMENT TO BE SEARCHED nw...: ?\n";
cin>>x;
b.srch(x);
cout<<"\n DO U FINISHED...???\n OR WANT TO SEARCH AGAIN ???(press Y / N)\n";
cin>>ch;
}
while(ch=='y');
}
//*****************ENJOY PROGRAMMING************************

//OUT PUT
//HOW MANY ELEMENTS TO BE PROCESSED...?
//2
//ENTER YOUR ELMENTS:...
//12
//21
//ENTER YOR ELEMENT TO BE SEARCHED nw...: ?
//12
// :)
// Element is found
// ELEMENT LOCATION IS 1
//DO U FINISHED...???
//OR WANT TO SEARCH AGAIN ???(press Y / N)
//y
//ENTER YOR ELEMENT TO BE SEARCHED nw...: ?
//30
//:( Element 30 is not found...!
//Please try again


No comments:

Post a Comment