Pagini recente » Cod sursa (job #2865279) | Cod sursa (job #483328) | Cod sursa (job #2848342) | Cod sursa (job #2189637) | Cod sursa (job #3238367)
#include <bits/stdc++.h>
using namespace std;
class Task {
public:
void solve() {
read_input();
get_result();
print_output();
}
private:
int n;
vector<int> v;
void read_input() {
ifstream fin("algsort.in");
fin >> n;
v.resize(n);
for (int i = 0; i < n; i++) {
fin >> v[i];
}
fin.close();
}
void threeWayPartition(int low, int high, int <, int >) {
int pivot = v[low];
lt = low;
gt = high;
int i = low;
while (i <= gt) {
if (v[i] < pivot) {
swap(v[lt], v[i]);
lt++;
i++;
} else if (v[i] > pivot) {
swap(v[i], v[gt]);
gt--;
} else {
i++;
}
}
}
void quickSort(int low, int high) {
if (low >= high) {
return;
}
int lt, gt;
threeWayPartition(low, high, lt, gt);
quickSort(low, lt - 1);
quickSort(gt + 1, high);
}
void get_result() {
quickSort(0, n - 1);
}
void print_output() {
ofstream fout("algsort.out");
for (int i = 0; i < n; i++) {
fout << v[i] << " ";
}
fout.close();
}
};
int main(void) {
ios::sync_with_stdio(false);
Task task;
task.solve();
return 0;
}