Tuesday 24 April 2012

7 th Experiment QUICK SORT


Algorithm for QUICK SORT
---------------------------------------------------------------------------------------------------------------------------------------
Step 1- START
Step 2-creat an Object for the class Quick
Step 3-  Do the following ….
a)      Display  the Options
b)      Read the  option
Step 4- 
a)      If the choice is” INSERT” then
a.1) Read the no of element to be Processed.
a.2) call the member function “insert” through  the obj.
a.3) accept the Elements 
a.4) Call the member function q_srt() by passing the parameter as the elements to the 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.2)RETURN
                c)if the choice is “EXIT” then
                                c.1)exit
                                until the choice is not exit, Repeat step-3 and step-4.
Step 5- STOP

---------------------------------------------------------------------------------------------------------------------------------------
FLOW CHARTs
---------------------------------------------------------------------------------------------------------------------------------------



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

//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(); }; //------------DEFINITION FOR CONSTRUCTOR------------ quick::quick(int z) { n=z; } //------------DEFINITION FOR DESTRUCTOR-------- quick::~quick() { //------------ } //--------------DEFINITION for getarr()------------- void quick::getarr() { cout<<"\n\t ENTER "<<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(); } //-----------DEFINITION 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); } } //-------DEFINITION 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; } //-----------DEFINITION 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; } //-------DEFINITION FOR output()----------- void quick::output() { cout<<"\n\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(); } OUTPUT: HOW MANY ELEMENTS TO BE PROCESSED...? 5 ENTER 5 ELEMENTS...: 6 4 1 2 6 ENTERED ELEMENTS ARE...: 6 4 1 2 6 QUICK SORT using CLASS ENTERED ARRAY SORTED ARRAY ~~~~~~~~~~~~~ ~~~~~~~~~~~~ 6 1 4 2 1 4 2 6 6 6

No comments:

Post a Comment