Pagini recente » Cod sursa (job #2636020) | Cod sursa (job #1440639) | Cod sursa (job #2174073) | Cod sursa (job #888133) | Cod sursa (job #2614861)
#include <iostream>
#include <fstream>
#include<cstdlib>
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];
}
}*/
void quick_like_sonic_sort(int a[], int left, int right)
{
int i=left, j=right, pivot=a[left+(rand()%(right-left))];
while (i<=j)
{
while (a[i]<pivot)
i++;
while (a[j]>pivot)
j--;
if (i<=j)
swap(a[i++],a[j--]);
}
if (i<right)
quick_like_sonic_sort(a,i,right);
if (j>left)
quick_like_sonic_sort(a,left,j);
}
int main()
{
int i,n;
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;
}