Sunday, April 22, 2012

QUICK SORT in array using CLASS


------------------------------------------------------------------------------------------------------------                             Algorithm for QUICK SORT using CLASS
------------------------------------------------------------------------------------------------------------
Step 1- START
Step 2-creat an Object for the class Quick
Step 3- Do           the following ….
a)      Display the Options
b)      Read the choice option
Step 4- 
a)      If the choice is” INSERT”        then
a.1) Read the no of element to be Processed.i.e(no)
a.2) call the member function “insert” through  the obj With passing the “no” as Parameter
a.3) accept the Elements 
a.4) Call the member function q_srt() with passing the parameter as the elements array
        which again call the function SUFFLE() & INTER CHANGE () and finally sort the Elements
a.5)Return
                b)  if the choice is “DISPLAY”
                                b.1) call the member function “DISPLAY” through the Object
                                b.3)RETURN
                c)if the choice is “EXIT”                  then
                                c.1) Display “ THANKS FOR USING PROGRAM” ,”PRESS ANY KEY….PLS!”
                                c.2)exit
UNTIL:L the choice is not equql to “EXIT”
Step 5- STOP
------------------------------------------------------------------------------------------------------------
                                          C ++ IMPLEMENTATION
------------------------------------------------------------------------------------------------------------
//sample program for QUICK SORT using CLASS
#include<iostream.h>
#include<conio.h>
class quick
{
private:
int arr[20],qk[20],v,n;
public:
quick(int);
~quick();
void getarr();
void q_srt(int ,int);
int part(int arr[],int i,int j);
void interchange(int arr[],int ,int);
void output();
};
//------------DEFINATION FOR CONSTRUCTOR------------
quick::quick(int z)
{
n=z;
}
//------------DEFINATON FOR DESTRUCTOR--------
quick::~quick()
{
//------------
}
//--------------DEFINATION for getarr()-------------
void quick::getarr()
{
cout<<"\n\tOKAY...NOW ENTER ur  "<<n<<"  ELEMENTS...:\n";
for(int i=1;i<=n;i++)
{
cin>>arr[i];
qk[i]=arr[i];
}
cout<<"\n\t YOUR ENTERED ELEMENTS ARE...:\n";
for(i=1;i<=n;i++)
{
cout<<"\t"<<arr[i];
}
getch();
}
//-----------DEFINATION for q_srt(int ,int)------------
void quick::q_srt(int p,int q)
{
int j;
if(p<q)
{
j=part(arr,p,q+1);
q_srt(j+1,q);
q_srt(p,j-1);
}
}
//-------DEFINATION for part(intarr[],int,int)_---------
int quick::part(int arr[],int x,int y)
{
v=arr[x];
int i=x;
int j=y;
do
{
do
{
i++;
}
while(arr[i]<v);
do
{
j--;
}
while(arr[j]>v);
if(i<j)
interchange(arr,i,j);
}
while(i<j);
arr[x]=arr[j];
arr[j]=v;
return j;
}
//-----------DEFINATION for interchange(arr[],int ,int)-----------
void quick::interchange(int arr[],int i,int j)
{
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
//-------DEFINATION FOR output()-----------
void quick::output()
{
clrscr();
cout<<"\n\t\t QUICK SORT using CLASS\n";
cout<<"\t\t~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\n\t ENTERED ARRAY "<<"\t\t SORTED ARRAY"<<endl;
cout<<"\t ~~~~~~~~~~~~~"<<"\t         ~~~~~~~~~~~~\n";
for(int i=1;i<=n;i++)

cout<<"\t   "<<qk[i]<<"\t\t\t\t"<<arr[i]<<endl;

getch();
}
//------------------MAIN FUNCTION-------------------
void main()
{
clrscr();
int no;
cout<<"\n\t HOW MANY ELEMENTS TO BE PROCESSED...?\n";
cin>>no;

quick qck(no);
qck.getarr();
qck.q_srt(1,no);
qck.output();
getch();
}
//--------------ENJOY PROGRAMMING------------------
------------------------------------------------------------------------------------------------------------
                                                             OUTPUT
------------------------------------------------------------------------------------------------------------

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


Friday, April 20, 2012


LINEAR SEARCH USING ARRAY

Algorithm:
Step-1: Start
Step-2: Accept no. of the values
Step-3: Accept the values and store it in an array
Step-4: Accept the no. to be searched as ‘num’
Step-5: If a value is found equal to ‘num’, output, ‘Element found’.
Step-6: If the value is not found, output, ‘Element not found’.
Step-7: Stop
-----------------------------------------------------------------------------------------------------------
                                                     FLOW CHART
-----------------------------------------------------------------------------------------------------------


------------------------------------------------------------------------------------------------------------
                                                  C++ IMPLEMENTATION
------------------------------------------------------------------------------------------------------------

//sample program for Linier search in Array
//*****************************************
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50],temp[50],i,j,item,no,t;
char ch;
cout<<"\n ENTER HOW MANY ELMENT TO BE PROCESSED:?\n";
cin>>no;
cout<<"\n ENTER YOUR ELEMENT\n";
for(i=1;i<=no;i++)
{
cin>>arr[i];
}
cout<<"\n THE ARRAY ELEMENTS ARE:...\n";
for(j=1;j<=no;j++)
{
cout<<"\t"<<arr[j]<<endl;
}


int a=1;
do
{
cout<<"\n\n\n\n\n ENTER YOUR ELMENT TO BE SEARCHED ?\n";
cin>>item;

do
{
while(arr[a]!=item)
{
a=a+1;
}
if(a>no)
{
cout<<"\n Search is unsucces full...\n";
}
else
{
int loc=a;
cout<<"\n Item is found in loation...: "<<loc<<endl;
}
}
while(i<no);

cout<<"\n DO YOU FINISHED...?\n OR YOU WANT TO USE PROGRAM again ?(press Y / N)\n";
cin>>ch;
}
while(ch=='y');
}
//*************ENJOY PROGRAMMING*************
//out put
//enter how many element u want to search...?
//3
//enter your nos...:
//15
//10
//20
//enter ur elemnt to search...?
//10
//the element is found in the position : 2
//do u want the program run again (y/n)
//y
//enter ur element to search...?
//30
//sorry...! your element is not found...: