Pagini recente » Cod sursa (job #2700776) | Cod sursa (job #357462) | Cod sursa (job #2715183) | Cod sursa (job #3125446) | Cod sursa (job #2614784)
#include <iostream>
#include <fstream>
#include <algorithm>
#include<cctype>
using namespace std;
ifstream fin ("algsort.in");
ofstream fout ("algsort.out");
int a[500000];
/*void mergesort( int st, int dr )
{
if(st!=dr)
{
int mij=((st+dr)>>1);
mergesort(st,mij);
mergesort(mij+1,dr);
int k,j,cpst=st;
k=st;
j=mij+1;
while(k<=mij && j<=dr)
if(v[k]<v[j])
a[st++]=v[k++];
else
a[st++]=v[j++];
while(k<=mij)
a[st++]=v[k++];
while(j<=dr)
a[st++]=v[j++];
for(j=cpst;j<=dr;j++)
v[j]=a[j];
}
}*/
int n;
void quick_like_sonic_sort(int v[], int left, int right)
{
if(left<right)
{
int pivot = left+(rand()%(right-left+1));
int b=left,e=right,aux;
while (v[b] < pivot)
b++;
while (v[e] > pivot)
e--;
while(b<e)
{
aux=v[b];
v[b]=v[e];
v[e]=aux;
do
b++;
while(v[b]<v[pivot] && b<e);
do
e--;
while(v[e]>v[pivot] && b<e);
}
if(e>left)
quick_like_sonic_sort(v,left,e);
if(e<right)
quick_like_sonic_sort(v,e+1,right);
}
}
int main()
{
int i;
fin >>n;
for(i=0;i<n;i++)
fin >> a[i];
quick_like_sonic_sort(a,0,n-1);
for(i=0;i<n;i++)
fout << a[i] << " ";
return 0;
}