Pagini recente » Cod sursa (job #1114112) | Cod sursa (job #2054708) | Cod sursa (job #563224) | Cod sursa (job #2580554) | Cod sursa (job #1490624)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;
const int maxSize = 1 << 20;
class pfstream {
private:
int cursor;
char buf[maxSize];
FILE *f;
inline void move() {
++ cursor;
if(cursor == maxSize) {
cursor = 0;
fread(buf, maxSize, 1, f);
}
}
inline int todig(char c) {
return c - '0';
}
public:
pfstream() {}
pfstream(const char *fname) {
f = fopen(fname, "r");
cursor = 0;
fread(buf, maxSize, 1, f);
}
inline pfstream &operator >> (int& N) {
char sign = '+';
while(!isdigit(buf[cursor])) {
sign = buf[cursor];
move();
}
N = 0;
while(isdigit(buf[cursor])) {
N = N * 10 + todig(buf[cursor]);
move();
}
if(sign == '-')
N = -N;
return *this;
}
inline pfstream &operator >> (float& N) {
char sign = '+';
while(!isdigit(buf[cursor])) {
sign = buf[cursor];
move();
}
N = 0;
while(isdigit(buf[cursor])) {
N = N * 10 + todig(buf[cursor]);
move();
}
if(buf[cursor] == '.') {
float p = 0.1;
move();
while(isdigit(buf[cursor])) {
N = N + p * todig(buf[cursor]);
p *= 0.1;
move();
}
}
if(sign == '-')
N = -N;
return *this;
}
};
void quickswap(int &a, int &b) {
if(a != b) {
a = a xor b;
b = a xor b;
a = a xor b;
}
}
const int MAX_N = 500000;
int v[MAX_N];
void quicksort(int *begin, int *end) {
int n = end - begin;
if(n > 1) {
int pivot = rand() % n;
quickswap(begin[pivot], begin[n - 1]);
pivot = begin[n - 1];
int i = 0;
for(register int j = 0; j < n - 1; ++ j)
if(begin[j] < begin[n - 1])
quickswap(begin[i], begin[j]), ++ i;
int i2 = i;
for(register int j = i2; j < n; ++ j) {
if(begin[j] == begin[n - 1])
quickswap(begin[i2], begin[j]), ++ i2;
}
quicksort(begin, begin + i);
quicksort(begin + i2, end);
}
}
int main() {
pfstream in("algsort.in");
freopen("algsort.out", "w", stdout);
int N;
in >> N;
for(register int i = 0; i < N; ++ i)
in >> v[i];
quicksort(v, v + N);
for(register int i = 0; i < N; ++ i)
printf("%d ", v[i]);
printf("\n");
return 0;
}