Cod sursa(job #2356085)

Utilizator AlexNiuclaeNiculae Alexandru Vlad AlexNiuclae Data 26 februarie 2019 14:51:04
Problema Sortare prin comparare Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>

using namespace std;

int compareElements(int a, int b) {
    //cntComparisons++;
    return a - b;
}

void swapElements(int &a, int &b) {
    //cntExchanges++;
    swap(a, b);
}

int quicksortPartition(vector<int> &arr, int left, int right) {
    int pivot = arr[right];
    int i = left;
    for (int j = left; j < right; ++j) {
        if (compareElements(arr[j], pivot) < 0) {
            swapElements(arr[i], arr[j]);
            i++;
        }
    }

    swapElements(arr[i], arr[right]);
    return i;
}

void quicksort(vector<int> &arr, int left, int right) {
    queue<pair<int,int>> q;
    q.push(make_pair(left, right));

    while (q.size()) {
        pair<int,int> segment = q.front(); q.pop();
        int pos = quicksortPartition(arr, segment.first, segment.second);
        if (segment.first <= pos - 1)
            q.push(make_pair(segment.first, pos - 1));
        if (pos + 1 <= segment.second)
            q.push(make_pair(pos + 1, segment.second));
    }
}

void runQuicksort(vector<int> &arr) {
    quicksort(arr, 0, (int)arr.size() - 1);

    /*SortInfo sortInfo;
    sortInfo.cntComparisons = cntComparisons;
    sortInfo.cntExchanges = cntExchanges;
    sortInfo.sortTime = ((double)time) / CLOCKS_PER_SEC;

    return sortInfo;*/
}

int main() {
    freopen("algsort.in","r",stdin);
    freopen("algsort.out","w",stdout);

    ios_base::sync_with_stdio(false);

    int N; cin >> N;

    vector<int> arr;
    for (int i = 0; i < N; ++i) {
        int x; cin >> x;
        arr.push_back(x);
    }

    runQuicksort(arr);
    for (auto &it: arr)
        cout << it << ' ';

    return 0;
}