Pagini recente » Cod sursa (job #1317673) | Cod sursa (job #359831) | Cod sursa (job #1440031) | Cod sursa (job #1589735) | Cod sursa (job #1490602)
#include <cstdio>
#include <cstring>
#include <algorithm>
const int MAX_N = 500000;
int N;
int V[MAX_N];
template<int SIZE>
class ReadStream {
private:
FILE* stream;
int pos;
char buffer[SIZE + 1];
public:
ReadStream(FILE* stream) {
this->stream = stream;
this->pos = SIZE;
}
int nextInt() {
int answer = 0;
int readSize;
while (true) {
if (this->pos == SIZE) {
readSize = fread(this->buffer, sizeof(char), SIZE, this->stream);
this->buffer[readSize] = 0;
this->pos = 0;
}
if ('0' <= this->buffer[this->pos] && this->buffer[this->pos] <= '9') {
break;
} else {
++this->pos;
}
}
while (true) {
if (this->pos == SIZE) {
readSize = fread(this->buffer, sizeof(char), SIZE, this->stream);
this->buffer[readSize] = 0;
this->pos = 0;
}
if (!('0' <= this->buffer[this->pos] && this->buffer[this->pos] <= '9')) {
break;
} else {
answer *= 10;
answer += this->buffer[this->pos] - '0';
++this->pos;
}
}
return answer;
}
char nextChar() {
int readSize;
if (this->pos == SIZE) {
readSize = fread(this->buffer, sizeof(char), SIZE, this->stream);
this->buffer[readSize] = 0;
this->pos = 0;
}
return this->buffer[this->pos++];
}
};
template<int SIZE>
class WriteStream {
private:
FILE* stream;
int pos;
char buffer[SIZE + 15];
public:
WriteStream(FILE* stream) {
this->stream = stream;
this->pos = 0;
}
void dump() {
if (pos > SIZE) {
fwrite(buffer, sizeof(char), pos, stream);
pos = 0;
}
}
void writeInt(int value) {
sprintf(buffer + pos, "%d%c", value, 0);
while (buffer[pos] != 0) {
pos++;
}
dump();
}
void writeChar(char value) {
buffer[pos] = value;
pos++;
dump();
}
~WriteStream() {
fwrite(buffer, sizeof(char), pos, stream);
}
};
void quicksort(int *begin, int *end) {
int n = end - begin;
if (n > 1) {
int pivot = n / 3;
std::swap(begin[pivot], begin[n - 1]);
pivot = begin[n - 1];
int *i = begin;
for (int *it = begin; it < end - 1; ++it) {
if (*it <= begin[n - 1]) {
std::swap(*i, *it);
++i;
}
}
std::swap(*i, begin[n - 1]);
quicksort(begin, i);
quicksort(i + 1, end);
}
}
int main(void) {
int i;
freopen("algsort.in", "r", stdin);
freopen("algsort.out", "w", stdout);
ReadStream<1000000> rs(stdin);
WriteStream<1000000> ws(stdout);
// citirea datelor
N = rs.nextInt();
//scanf("%d", &N);
for (i = 0; i < N; ++i) {
V[i] = rs.nextInt();
//scanf("%d", &V[i]);
}
// calcularea solutiei
quicksort(V, V + N);
// afisarea solutiei
for (i = 0; i < N; ++i) {
ws.writeInt(V[i]);
ws.writeChar(' ');
//printf("%d ", V[i]);
}
ws.writeChar('\n');
//printf("\n");
return 0;
}