Cod sursa(job #1833641)

Utilizator crazylamaRiclea Andrei crazylama Data 22 decembrie 2016 20:49:35
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 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)
{
    int i = st, j = dr;
    int pivot = st + (dr - st) / 2;
    unsigned int x = v[pivot];
    while (i <= j)
    {
        while (i < dr && v[i] < x)
            i++;
        while(j > st && 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;
}