Pagini recente » Cod sursa (job #2234160) | Cod sursa (job #409236) | Cod sursa (job #3126882) | Cod sursa (job #1319032) | Cod sursa (job #2356085)
#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;
}