Pagini recente » Cod sursa (job #2171977) | Cod sursa (job #2975687) | Cod sursa (job #586100) | Cod sursa (job #3277094) | Cod sursa (job #2614810)
#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 v[], int left, int right)
{
if(left<right)
{
int pivot = left+(rand()%(right-left+1));
int b=left,e=right,aux;
while (v[b] < v[pivot] && b<e)
b++;
while (v[e] > v[pivot] && e>b)
e--;
cout << e << " " << b << " ";
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,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;
}