Cod sursa(job #1307487)

Utilizator diana-t95FMI Tudoreanu Diana Elena diana-t95 Data 2 ianuarie 2015 13:42:08
Problema Sortare prin comparare Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <time.h>
using namespace std;
#define maxn 50001
int n, a[maxn];
void qsort(int st, int dr)
{
    if (st>=dr) return;
    if (st+1==dr) {if (a[st]>a[dr]) swap(a[st], a[dr]); return;}
    int ind, i, j, m;
    ind=rand() %(dr-st) + st + 1;
    m=a[ind];
    i=st; j=dr;
    while(i<j)
    {
        while (a[i]<m) i++;
        while (a[j]>m) j--;
        if (i<j) {swap(a[i], a[j]); i++; j--;}
    }
    qsort(st, i);
    qsort(i, dr);
}
int main()
{
    ifstream f("algsort.in");
    ofstream g("algsort.out");
    srand(time(NULL));
    f>>n;
    int i;
    for (i=1;i<=n;i++) f>>a[i];
    qsort(1, n);
    for (i=1;i<=n;i++) g<<a[i]<< ' ';
    g<<'\n';
}