#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define size 10
void quick(int a[size],int,int);
int partition(int a[size],int,int);
void swap(int a[size],int *,int *);
int n;
int main()
{
int i,a[size];
clrscr();
cout<<"Quick Sort Method";
cout<<"Enter total numbers to sort";
cin>>n;
cout<<"Enter the elements";
for(i=0;i<n;i++)
{
cin>>a[i] ;
}
quick(a,0,n-1);
cout<<"Sorted Array";
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
getch();
return 0;
}
//This function is to sort the elements in a sublist
void quick(int a[size],int low,int high)
{
int m,i;
if(low<high)
{
m=partition(a,low,high);
quick(a,low,m-1);
quick(a,m+1,high);
}
}
//This function is to partition a list and decide the pivot element
int partition(int a[size],int low,int high)
{
int pivot=a[low],i=low,j=high;
while(i<=j)
{
while(a[i]<=pivot)
i++;
while(a[j]>pivot)
j--;
if(i<j)
swap(a,&i,&j);
}
swap(a,&low,&j);
return j;
}
void swap(int a[size],int *i,int *j)
{
int temp;
temp=a[*i];
a[*i]=a[*j];
a[*j]=temp;
}
#include<conio.h>
#include<stdlib.h>
#define size 10
void quick(int a[size],int,int);
int partition(int a[size],int,int);
void swap(int a[size],int *,int *);
int n;
int main()
{
int i,a[size];
clrscr();
cout<<"Quick Sort Method";
cout<<"Enter total numbers to sort";
cin>>n;
cout<<"Enter the elements";
for(i=0;i<n;i++)
{
cin>>a[i] ;
}
quick(a,0,n-1);
cout<<"Sorted Array";
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
getch();
return 0;
}
//This function is to sort the elements in a sublist
void quick(int a[size],int low,int high)
{
int m,i;
if(low<high)
{
m=partition(a,low,high);
quick(a,low,m-1);
quick(a,m+1,high);
}
}
//This function is to partition a list and decide the pivot element
int partition(int a[size],int low,int high)
{
int pivot=a[low],i=low,j=high;
while(i<=j)
{
while(a[i]<=pivot)
i++;
while(a[j]>pivot)
j--;
if(i<j)
swap(a,&i,&j);
}
swap(a,&low,&j);
return j;
}
void swap(int a[size],int *i,int *j)
{
int temp;
temp=a[*i];
a[*i]=a[*j];
a[*j]=temp;
}