Cod sursa(job #2197334)

Utilizator VladTiberiuMihailescu Vlad Tiberiu VladTiberiu Data 21 aprilie 2018 19:23:22
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");

const int NMax = 500001;

int a[NMax];
int n;

int myPartition(int l, int r){
    int y = rand();
    y = y * rand();
    int position = y % (r - l + 1) + l;

    swap(a[position],a[r]);

    int i = l - 1;
    int j = l;
    for(j = l; j < r; ++j){
        if(a[j] <= a[r]){
            swap(a[j],a[i + 1]);
            i += 1;
        }
    }
    swap(a[i + 1], a[r]);
    return i + 1;
}
void myQuickSort(int l, int r){
    if(l >= r){
        return;
    }
    int position = myPartition(l,r);
    myQuickSort(l, position - 1);
    myQuickSort(position + 1, r);
}
int main()
{
    f >> n;
    srand(time(NULL));
    for(int i = 1; i <= n; ++i){
        f >> a[i];
    }
    myQuickSort(1, n);
    for(int i = 1; i <= n; ++i){
        g << a[i] << ' ';
    }
    return 0;
}