Pagini recente » Cod sursa (job #1038473) | Cod sursa (job #2194995) | Cod sursa (job #1942701) | Cod sursa (job #456204) | Cod sursa (job #2355660)
#include <iostream>
#include <vector>
#include <cstdio>
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) {
if (left >= right)
return;
int pos = quicksortPartition(arr, left, right);
quicksort(arr, left, pos - 1);
quicksort(arr, pos + 1, right);
}
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);
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;
}