Cod sursa(job #2159647)

Utilizator tudoroprisTudor Opris tudoropris Data 11 martie 2018 07:58:57
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include "stdafx.h"
#include <fstream>

const int MaxN = 500005;

using namespace std;

ifstream cin("algsort.in");
ofstream cout("algsort.out");

int v[MaxN];

void qsort(int l, int r) {
	int i = l, j = r, m = v[(l + r) / 2], aux;
	while (i <= j) {
		while (v[i]<m)i++;
		while (v[j]>m)j--;
		if (i <= j) {
			swap(v[i], v[j]);
			i++;
			j--;
		}
	}
	if (l<j)
		qsort(l, j);
	if (r>i)
		qsort(i, r);
}

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> v[i];
	qsort(0, n - 1);
	for (int i = 0; i < n; i++)
		cout << v[i] << ' ';
	return 0;
}