#include <fstream>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
int poz(int a[50000], int s, int d)
{
int i,j,di,dj,aux;
i=s; j=d; di=0; dj=1;
while(i<j)
{
if(a[i]>a[j])
{
aux=a[i]; a[i]=a[j]; a[j]=aux;
di=1-di; dj=1-dj;
}
i=i+di;
j=j-dj;
}
return j;
}
void QS(int a[50000], int s, int d)
{
int p;
if(s<d)
{
p=poz(a,s,d);
QS(a,s,p-1);
QS(a,p+1,d);
}
}
void citire(int a[50000], int &n)
{
int i;
fin>>n;
for(i=1;i<=n;i++) fin>>a[i];
}
void afis(int a[50000], int n)
{
int i;
for(i=1;i<=n;i++) fout<<a[i]<<"\n";
}
int main()
{
int a[50000], n;
citire(a,n);
QS(a,1,n);
afis(a,n);
return 0;
}
sau
#include<iostream.h>
int a[100],n;
void QS(int x , int y)//Quicksort
{
if (x < y)
{
int i, j,aux,m;
m = a[(x+y)/2];
i = x-1;
j = y+1;
while (i < j)
{
do j--;
while (a[j] > m);
do i++;
while (a[i] < m);
if (i < j)
{
int aux;
aux = a[i];
a[i] = a[j];
a[j] = aux;
}
}
QS(x, j);
QS(j+1,y);
}
}
void main()
{ int i;
cin>>n;
for(i=1;i<=n;i++) cin>>a[i];
QS(1, n);
for(i=1;i<=n;i++) cout<<a[i]<<" ";
}
|