Cod sursa(job #2104666)

Utilizator SlevySlevoaca Stefan-Gabriel Slevy Data 12 ianuarie 2018 02:43:23
Problema Sortare prin comparare Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#define NMAX 500001

using namespace std;

int a[NMAX], n;

void read() {

	ifstream in("algsort.in");

	in >> n;
	for (int i = 1; i <= n; i++)
		in >> a[i];
	
	in.close();
}

void quicksort(int x, int y) {

	if (x < y) {

		int i = x, j = y;
		int pos = x + (y - x) / 2;

		swap(a[i], a[pos]);

		int depX = 0, depY = -1;
		while (i < j) {

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

				swap(a[i], a[j]);
				int aux = depX;
				depX = -depY;
				depY = -aux;
			}
			i += depX;
			j += depY;
		}

		quicksort(x, i - 1);
		quicksort(i + 1, y);
	}
}

int main() {

	read();
	quicksort(1, n);

	ofstream out("algsort.out");
	for (int i = 1; i <= n; i++)
		out << a[i] << " ";
	out.close();
}