Cod sursa(job #3242116)

Utilizator CondoracheAlexandruCondorache Alexandru CondoracheAlexandru Data 9 septembrie 2024 10:38:44
Problema Sortare prin comparare Scor 40
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <stdio.h>
#include <stdlib.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("popcnt,avx2")
#define ll long long
const int maxn = 500005;


int partition(int* a, int low, int high) {
    int pivot = a[low];
    int i = low - 1;
    int j = high + 1;
    while (1) {
        do {
            i++;
        } while (a[i] < pivot);
        do {
            j--;
        } while (a[j] > pivot);
        if (i >= j) {
            return j;
        }
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}

void quick_sort(int* a, int low, int high) {
    if (low >= high) {
        return;
    }
    int pivot = partition(a, low, high);
    quick_sort(a, low, pivot); 
    quick_sort(a, pivot + 1, high);
}

void solve() {
    FILE* in = fopen("algsort.in", "r");
    FILE* out = fopen("algsort.out", "w");
    int n;
    fscanf(in, "%d", &n);
    int* a = malloc(n * sizeof(int));
    for (int i = 0; i < n; i++) {
        fscanf(in, "%d", &a[i]);
    }
    quick_sort(a, 0, n - 1);
    for (int i = 0; i < n; i++) {
        fprintf(out, "%d ", a[i]);
    }
}
 
int main() {
    int t = 1;
    // fin >> t;
    while (t--) {
        solve();
    }
    return 0;
}