Cod sursa(job #599614)
| Utilizator | Data | 29 iunie 2011 11:48:16 | |
|---|---|---|---|
| Problema | Sortare prin comparare | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.46 kb |
#include<iostream.h>
#include<fstream.h>
int a[500000];
void sort(int l,int r)
{int i, j, x, y;
i=l; j=r; x=a[(l+r)/2];
while (i<=j)
{ while (a[i]<x) {i=i+1;}
while (x<a[j]) {j=j-1;}
if (i<=j)
{y=a[i]; a[i]=a[j]; a[j]=y;
i=i+1; j=j-1;}
}
if (l<j) sort(l,j);
if (i<r) sort(i,r);
}
int main()
{int i,n;
ifstream f("algsort.in");
ofstream h("algsort.out");
f>>n;
for (i=1;i<=n;i++)
f>>a[i];
sort(1,n);
for (i=1;i<=n;i++)
h<<a[i]<<" ";
return 0;
} 