Cod sursa(job #2031927)

Utilizator tudormaximTudor Maxim tudormaxim Data 4 octombrie 2017 00:39:05
Problema Sortare prin comparare Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.92 kb
#include <stdio.h>
#include <stdlib.h>

const int maxn = 5e5 + 5;

void Quicksort(int V[], int left, int right) {
    int i = left;
    int j = right;
    int pivot = V[((left + right) >> 1)];
    int tmp;
    while (i <= j) {
        while (V[i] < pivot) i++;
        while (V[j] > pivot) j--;
        if (i <= j) {
            tmp = V[i];
            V[i] = V[j];
            V[j] = tmp;
            i++;
            j--;
        }
    }
    if (left < j) Quicksort(V, left, j);
    if (i < right) Quicksort(V, i, right);

}

int main (void) {
    freopen("algsort.in", "r", stdin);
    freopen("algsort.out", "w", stdout);
    int V[maxn], n, i;
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        scanf("%d", &V[i]);
    }
    Quicksort(V, 0, n - 1);
    for (i = 0; i < n; i++) {
        printf("%d ", V[i]);
    }
    printf("\n");
    fclose(stdin);
    fclose(stdout);
    return 0;
}