Cod sursa(job #2061030)

Utilizator dobrebogdanDobre Bogdan Mihai dobrebogdan Data 8 noiembrie 2017 21:16:07
Problema Sortare prin comparare Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<ctime>
using namespace std;
const int nmax=500000;
void quicks(int *v,int st,int dr)
{
    if(st>=dr)
        return;
    int piv=v[(rand()%(dr-st+1))+st];
    int poz=st-1;
    int i;
    for(i=st; i<=dr; i++)
        if(v[i]<=piv)
        {
            poz++;
            swap(v[poz],v[i]);
        }
        for(i=st;i<=poz;i++)
        if(v[i]==piv)
        {
        swap(v[i],v[poz]);
        break;
        }
         //   printf("%d %d %d\n",st,dr,poz);
    quicks(v,st,poz-1);
    quicks(v,poz+1,dr);
}
int main()
{
    freopen("algsort.in","r",stdin);
    freopen("algsort.out","w",stdout);
    int i,n,ve[nmax+5];
    scanf("%d",&n);
    for(i=1; i<=n; i++)
        scanf("%d",&ve[i]);
    quicks(ve,1,n);
    for(i=1;i<=n;i++)
    printf("%d ",ve[i]);
    return 0;
}