Cod sursa(job #1490614)

Utilizator spatarelDan-Constantin Spatarel spatarel Data 23 septembrie 2015 21:10:00
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.81 kb
#include <cstdio>
#include <cstring>
#include <ctime>

#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 writeUnsignedInt(unsigned int value) {
        if (value == 0) {
            buffer[pos] = '0';
            pos++;
        } else {
            int newPos = pos;
            while(value > 0) {
                buffer[newPos] = '0' + (value % 10);
                newPos++;
                value /= 10;
            }
            std::reverse(buffer + pos, buffer + newPos);
            pos = newPos;
        }
        dump();
    }

    void writeChar(char value) {
        buffer[pos] = value;
        pos++;
        dump();
    }

    ~WriteStream() {
        fwrite(buffer, sizeof(char), pos, stream);
    }
};

void quickswap(int &a, int &b) {
  if (a != b) {
    a = a xor b;
    b = a xor b;
    a = a xor b;
  }
}

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 (int j = 0; j < n - 1; ++j) {
      if (begin[j] < begin[n - 1]) {
        quickswap(begin[i], begin[j]);
        ++i;
      }
    }
    int ii = i;
    for (int j = ii; j < n; ++j) {
      if (begin[j] == begin[n - 1]) {
        quickswap(begin[ii], begin[j]);
        ++ii;
      }
    }

    quicksort(begin, begin + i);
    quicksort(begin + ii, end);
  }
}

int main(void) {
    int i;
    srand(time(NULL));

    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.writeUnsignedInt(V[i]);
        ws.writeChar(' ');
        //printf("%d ", V[i]);
    }
    ws.writeChar('\n');
    //printf("\n");
    return 0;
}