Cod sursa(job #2760176)

Utilizator dey44andIoja Andrei-Iosif dey44and Data 23 iunie 2021 14:54:32
Problema Sortare prin comparare Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
#include <ctime>
#include <cstdlib>

using namespace std;

ifstream cin("algsort.in");
ofstream cout("algsort.out");

int a[500005], N;

int Lomuto_Part(int L, int R)
{
    int p = L + rand() % (R - L);
    swap(a[L], a[p]);
    int st = L, dr = R, x = a[st];
    while(st < dr)
    {
        while(st < dr && a[dr] >= x) dr--;
        a[st] = a[dr];
        while(st < dr && a[st] <= x) st++;
        a[dr] = a[st];
    }
    a[st] = x;
    return st;
}

void QuickSort(int L, int R)
{
    int M = Lomuto_Part(L, R);
    if(M - L > 1) QuickSort(L, M - 1);
    if(R - M > 1) QuickSort(M + 1, R);
}

int main()
{
    srand(time(NULL));
    cin >> N;
    for(int i = 1; i <= N; i++)
        cin >> a[i];
    QuickSort(1, N);
    for(int i = 1; i <= N; i++)
        cout << a[i] << " ";
    return 0;
}