Pagini recente » Clasamentul arhivei de probleme | Cod sursa (job #3144240) | Cod sursa (job #2904521) | Cod sursa (job #2386662)
#include <fstream>
#include <stack>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int N;
stack <int> stk, aux_stk;
void sort(stack <int>& S) {
if (S.empty()) {
return;
}
int pivot = S.top();
S.pop();
stack <int> left;
stack <int> right;
while (!S.empty()) {
int y = S.top();
if (y < pivot) {
left.push(y);
} else {
right.push(y);
}
S.pop();
}
sort(left);
sort(right);
stack <int> tmp;
while (!right.empty()) {
tmp.push(right.top());
right.pop();
}
tmp.push(pivot);
while (!left.empty()) {
tmp.push(left.top());
left.pop();
}
while (!tmp.empty()) {
S.push(tmp.top());
tmp.pop();
}
}
int main() {
fin >> N;
for (int x, i = 0; i < N; ++i) {
fin >> x;
aux_stk.push(x);
}
sort(aux_stk);
while (!aux_stk.empty()) {
stk.push(aux_stk.top());
aux_stk.pop();
}
while (!stk.empty()) {
fout << stk.top() << ' ';
stk.pop();
}
fout << '\n';
return 0;
}