Cod sursa(job #2620173)

Utilizator ioanapintilie07Pintilie Ioana ioanapintilie07 Data 28 mai 2020 16:03:33
Problema Sortare prin comparare Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int pivot(int v[], int st, int dr) {
    int x, aux, p;
    p = rand() % (dr - st) + st;
    aux = v[p];
    v[p] = v[st];
    v[st] = aux;
    x = v[st];
    while (st < dr) {
        while (st < dr && v[dr] > x)dr--;
        v[st] = v[dr];
        while (st < dr && v[st] <= x)st++;
        v[dr] = v[st];
    }
    v[st] = x;
    return st;
}


void quickSort(int v[], int st, int dr) {
    int p;
    if (st < dr) {
        p = pivot(v, st, dr);
        quickSort(v, st, p - 1);
        quickSort(v, p + 1, dr);
    }
}

int main() {
    ifstream fin("algsort.in");
    ofstream fout("algsort.out");
    int n, v[500001], i;
    fin >> n;
    for(i = 1; i <= n; ++i)
        fin >> v[i];
    quickSort(v, 1, n);
    for(i = 1; i <= n; ++i)
        fout << v[i] << " ";
    return 0;
}