Cod sursa(job #2618814)

Utilizator radustn92Radu Stancu radustn92 Data 26 mai 2020 12:47:21
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstdlib>
#include <ctime>
using namespace std;

const int NMAX = 500505;
int N, A[NMAX], aux[NMAX];

void sortArray(int A[], int from, int to) {
	if (from == to) {
		return;
	}

	int mid = (from + to) / 2l;
	sortArray(A, from, mid);
	sortArray(A, mid + 1, to);

	int i = from, j = mid + 1, nextPos = from;
	while (i <= mid && j <= to) {
		if (A[i] < A[j]) {
			aux[nextPos++] = A[i++];
		} else {
			aux[nextPos++] = A[j++];
		}
	}
	while (i <= mid) {
		aux[nextPos++] = A[i++];
	}
	while (j <= to) {
		aux[nextPos++] = A[j++];
	}
	copy(aux + from, aux + to + 1, A + from);
}

int main() {
	freopen("algsort.in", "r", stdin);
	freopen("algsort.out", "w", stdout);

	srand(time(0));

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> N;
	for (int idx = 1; idx <= N; idx++) {
		cin >> A[idx];
	}

	sortArray(A, 1, N);
	for (int idx = 1; idx <= N; idx++) {
		if (idx > 1) {
			cout << " ";
		}
		cout << A[idx];
	}
	cout << "\n";
	return 0;
}