0% found this document useful (0 votes)
6 views3 pages

15) Quick Sort

Uploaded by

lsomesh448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

15) Quick Sort

Uploaded by

lsomesh448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

NAME : Nikita Aasaram Patil

ROLL NO : 101
PRACTICAL NO : 4.7
PRACTICAL TITLE : Implementation of Quick sort using array
---------------------------------------------------------------

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class LIST
{
int *A,n;
public:
LIST(int);
void READ_LIST_101();
void SHOW_LIST_101();
void QUICK_SORT_ASC_101(int,int);
void QUICK_SORT_DESC_101(int,int);
int PARTITION_ASC_101(int,int);
int PARTITION_DESC_101(int,int);

};
LIST::LIST(int par)
{
n=par;
A=new int[n+2];
}
void LIST::READ_LIST_101()
{
cout<<endl<<"Enter elements : ";
for(int i=1;i<=n;i++)
{
cin>>A[i];
//A=random(9099);
}
A[i]=9999; // positive infinity
}
void LIST::SHOW_LIST_101()
{
cout<<endl;
for(int i=1;i<=n;i++)
cout<<A[i]<<" ";
}
void LIST::QUICK_SORT_ASC_101(int p,int q)
{
if(p<q)
{
int j=q+1;
j=PARTITION_ASC_101(p,j);
QUICK_SORT_ASC_101(p,j-1);
QUICK_SORT_ASC_101(j+1,q);
}
}
void LIST::QUICK_SORT_DESC_101(int p, int q)
{
if(p<q)
{
int j=q+1;
j=PARTITION_DESC_101(p,j);
QUICK_SORT_DESC_101(p,j-1);
QUICK_SORT_DESC_101(j+1,q);
}
}
int LIST::PARTITION_ASC_101(int m,int p)
{
int temp =A[m];
int i=m;
do
{
do
{
i=i+1;
}while(A[i]<temp);
do
{
p=p-1;
}while(A[p]>temp);
if(i<p)
{
int t=A[i];A[i]=A[p];A[p]=t;
}
else
break;
}while(1);
A[m]=A[p];
A[p]=temp;
return p;
}
int LIST::PARTITION_DESC_101(int m,int p)
{
int temp =A[m];
int i=m;
do
{
do
{
i=i+1;
}while(A[i]>temp);
do
{
p=p-1;
}while(A[p]<temp);
if(i<p)
{
int t=A[i];A[i]=A[p];A[p]=t;
}
else
break;
}while(1);
A[m]=A[p];
A[p]=temp;
return p;
}
void main()
{
int no;
clrscr();
cout<<"Enter size of array : ";
cin>>no;
LIST obj(no);
obj.READ_LIST_101();
cout<<endl<<"List Before sorting ";
obj.SHOW_LIST_101();
obj.QUICK_SORT_ASC_101(1,no);
cout<<endl<<"list after sorting in ascending order : ";
obj.SHOW_LIST_101();
obj.QUICK_SORT_DESC_101(1,no);
cout<<endl<<"list after sorting in descending order : ";
obj.SHOW_LIST_101();
getch();

You might also like