Cod sursa(job #1363655)

Utilizator RazvanR104Razvan-Andrei Ciocoiu RazvanR104 Data 27 februarie 2015 09:36:36
Problema Sortare prin comparare Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <iomanip>
#include <fstream>

#include <algorithm>
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#if __cplusplus > 199711L
#include <unordered_map>
#include <unordered_set>
#endif

#include <cstdio>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdlib>

using namespace std;

typedef long long int64;

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

const int NMAX = 500010;

int N;
int V[NMAX];

void Swap(int &lhs, int &rhs) {
    int aux = lhs;
    lhs = rhs;
    rhs = aux;
}

int Partition(int left, int right) {
    int pivot_index = left + rand() % (right - left + 1), pos = left;
    Swap(V[pivot_index], V[right]);

    for (int i = left; i < right; ++i)
        if (V[i] < V[right])
            Swap(V[i], V[pos++]);

    Swap(V[right], V[pos]);
    return pos;
}

void QuickSort(int left, int right) {
    if (left >= right) return;

    int Part = Partition(left, right);
    QuickSort(left, Part);
    QuickSort(Part + 1, right);
}

int main() {
    int i;
    srand(time(NULL));

    in >> N;
    for (i = 1; i <= N; ++i) in >> V[i];

    QuickSort(1, N);
    //sort(V + 1, V + N + 1);
    for (i = 1; i <= N; ++i) out << V[i] << ' ';
    out << '\n';

    in.close(), out.close();
    return 0;
}