Cod sursa(job #1490559)

Utilizator tudorcomanTudor Coman tudorcoman Data 23 septembrie 2015 19:35:47
Problema Sortare prin comparare Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 2.17 kb

#pragma one
#include <iostream>
#include <stdio.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) {
    a = a xor b;
    b = a xor b;
    a = a xor b;
}

void quicksort(int *begin, int *end) {
    if (begin < (end - 1)) {
        int n = end - begin;
        quickswap(*begin, begin[n >> 1]);
        int *i = begin, *j = end - 1, d = 0;

        while(i < j) {
            if(*i > *j) {
                quickswap(*i, *j);
                d = 1 - d;
            }

            i += d;
            j -= 1 - d;
        }

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

const int MAX_N = 500000;
int v[MAX_N];
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;
}