/* sap xep qick sort*/
#include <iostream.h>
#include <conio.h>
#define max 100
void nhapmang(int a[],int n)
{ for(int i=0;i<n;i++)
{ cout<<" nhap ptu a["<<i<<"]:=";
cin>>a[i];
}
}
void xuatmang(int a[],int n)
{ for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
}
void swap(int &a,int &b)
{ int temp=a;
a=b;
b=temp;
}
void quick_sort(int a[],int Left,int Right)
{ int i=Left,j=Right;
int pivot=a[(Left+Right)/2];
while(i<j)
{
while(a[i]<pivot)
i++;
while(a[j]>pivot)
i--;
if(i<=j)
{
swap(a[i],a[j]);
i++;
j--;
}
}
if(Left<j)
quick_sort(a,Left,j);
if(i<Right)
quick_sort(a,i,Right);
}
void main()
{ clrscr();
int a[max],n;
cout<<"nhap n:=";
cin>>n;
nhapmang(a,n);
cout<<"xuat mang vua nhap:";
xuatmang(a,n);
quick_sort(a,0,n-1);
cout<<"mang vua sap xep la:";
xuatmang(a,n);
getch();
}
#include <iostream.h>
#include <conio.h>
#define max 100
void nhapmang(int a[],int n)
{ for(int i=0;i<n;i++)
{ cout<<" nhap ptu a["<<i<<"]:=";
cin>>a[i];
}
}
void xuatmang(int a[],int n)
{ for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
}
void swap(int &a,int &b)
{ int temp=a;
a=b;
b=temp;
}
void quick_sort(int a[],int Left,int Right)
{ int i=Left,j=Right;
int pivot=a[(Left+Right)/2];
while(i<j)
{
while(a[i]<pivot)
i++;
while(a[j]>pivot)
i--;
if(i<=j)
{
swap(a[i],a[j]);
i++;
j--;
}
}
if(Left<j)
quick_sort(a,Left,j);
if(i<Right)
quick_sort(a,i,Right);
}
void main()
{ clrscr();
int a[max],n;
cout<<"nhap n:=";
cin>>n;
nhapmang(a,n);
cout<<"xuat mang vua nhap:";
xuatmang(a,n);
quick_sort(a,0,n-1);
cout<<"mang vua sap xep la:";
xuatmang(a,n);
getch();
}