Cod sursa(job #1833630)

Utilizator crazylamaRiclea Andrei crazylama Data 22 decembrie 2016 20:31:26
Problema Sortare prin comparare Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <cstdio>
#include <cstdlib>
using namespace std;

FILE *f=fopen("algsort.in","r");
FILE *g=fopen("algsort.out","w");

void QuickSort(unsigned int *v, int st, int dr)
{
    if(st <= dr)
    {
        int i = st, j = dr;
        int pivot = st + (dr - st) / 2;
        unsigned int x = v[pivot];
        while (i <= j)
        {
            while (v[i] < x)
                i++;
            while(v[j] > x)
                j--;
            if (i <= j)
            {
                int aux = v[i];
                v[i] = v[j];
                v[j] = aux;
            }
            i++;
            j--;
            if (st < j)
                QuickSort(v, st, j);
            if (i < dr)
                QuickSort(v, i, dr);
        }
    }
}

int main()
{
    int n;
    unsigned int *v;
    fscanf(f, "%d", &n);
    v = (unsigned int*)calloc(n + 1, sizeof(unsigned int));
    for (int i = 0; i < n; i++)
        fscanf(f, "%u", &v[i]);
    QuickSort(v, 0, n - 1);
    for (int i = 0; i < n; i++)
        fprintf(g, "%u ", v[i]);
    return 0;
}