Cod sursa(job #3255536)

Utilizator AdrianRosuRosu Adrian Andrei AdrianRosu Data 10 noiembrie 2024 22:30:57
Problema Sortare prin comparare Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.76 kb
#include <bits/stdc++.h>

using namespace std;

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

InParser fin("algsort.in");

ofstream fout("algsort.out");

int n , v[500005];

void QuickSort(int st, int dr){

    if(st < dr){

        int m = (st + dr) / 2;

        int aux = v[st];

        v[st] = v[m];

        v[m] = aux;

        int i = st , j = dr, d = 0;

        while(i < j){

            if(v[i] > v[j]){

                aux = v[i];

                v[i] = v[j];

                v[j] = aux;

                d = 1 - d;

            }

            i += d;

            j -= 1 - d;

        }

        QuickSort(st , i - 1);

        QuickSort(i + 1 , dr);

    }

}

int main(){

    fin >> n;

    for(int i = 0 ; i < n ; i++)

        fin >> v[i];

    QuickSort(0 , n - 1);

    for(int i = 0 ; i < n ; i++)

        fout << v[i] << " ";

    return 0;
}