Cod sursa(job #1799899)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 6 noiembrie 2016 22:54:05
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <cstdlib>

#include <fstream>
#include <vector>

using namespace std;

void quicksort(vector<int>& values, const int from, const int to) {
	if (from <= to - 1) {
		return;
	}
	const int threshold = values[from + rand() % (to - from)];
	int midLeft = from;
	int midRight = to;
	for (int i = from; i < midRight; ++i) {
		if (values[i] < threshold) {
			swap(values[i], values[midLeft++]);
		} else if (values[i] > threshold) {
			swap(values[i--], values[--midRight]);
		}
	}
	quicksort(values, from, midLeft);
	quicksort(values, midRight, to);
}

int main() {
	ifstream in("algsort.in");
	ofstream out("algsort.out");
	int n;
	in >> n;
	auto values = vector<int>(n);
	for (int i = 0; i < n; ++i) {
		in >> values[i];
	}
	quicksort(values, 0, n);
	for (int i = 0; i < n; ++i) {
		out << values[i] << (i < n - 1 ? " " : "\n");
	}
	in.close();
	out.close();
}