█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 1955

Problema 0xde - Ordenar un arreglo grande

  • Autor: Javier
  • Fecha: 2020-11-12 02:01:42 UTC (Hace alrededor de 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.007 s 1 KBi
#2
Correcto
0.007 s 1 KBi
#3
Correcto
0.006 s 1 KBi
#4
Correcto
0.006 s 5 KBi
#5
Correcto
0.007 s 1 KBi
#6
Correcto
0.006 s 1 KBi
#7
Correcto
0.052 s 2 KBi
#8
Correcto
0.051 s 2 KBi
#9
Correcto
0.05 s 2 KBi
#10
Correcto
0.069 s 2 KBi
#11
Correcto
0.097 s 3 KBi
#12
Correcto
0.098 s 3 KBi
#13
Correcto
0.074 s 2 KBi
#14
Correcto
0.075 s 2 KBi
#15
Correcto
0.058 s 3 KBi
#16
Correcto
0.083 s 3 KBi
#17
Correcto
0.103 s 3 KBi
#18
Correcto
0.068 s 2 KBi
#19
Correcto
0.06 s 2 KBi
#20
Correcto
0.052 s 2 KBi
#21
Correcto
0.067 s 3 KBi
#22
Correcto
0.067 s 2 KBi
#23
Correcto
0.064 s 2 KBi
#24
Correcto
0.071 s 2 KBi
#25
Correcto
0.06 s 3 KBi
#26
Correcto
0.068 s 3 KBi
#27
Correcto
0.073 s 2 KBi
Puntos totales: 100 / 100

Código

#include <iostream>
#include <vector>

using namespace std;

void merge(vector<int> &a, int left, int middle, int right) {
  // create temporal arrays
  int leftLength = middle - left + 1; 
  int rightLength = right - middle; 
  int arrLeft[leftLength];
  int arrRight[rightLength];
	 
  // fill temporal arrays
  for (int i = 0; i < leftLength; i++) {
	 arrLeft[i] = a[left + i]; 
  }
  for (int i = 0; i < rightLength; i++) {
	 arrRight[i] = a[middle + 1 + i]; 
  }

  // replace sorted values in original array 
  int l = 0, r = 0, o = left;
  // iterate over both array's
  while (l < leftLength and r < rightLength) {
	 if(arrLeft[l] <= arrRight[r]) {
		a[o++] = arrLeft[l++];
	 } else {
		a[o++] = arrRight[r++];
	 }
  }
  // if there are items left in either temp array, transfer them
  while (l<leftLength) {
	 a[o++] = arrLeft[l++];
  }
  while (r<rightLength) {
	 a[o++] = arrRight[r++];
  }
}

void do_merge_sort(vector<int> &a, int left, int right) {
  if (left < right) {
	 int middle = left + (right - left)/2;
	 do_merge_sort(a, left, middle);
	 do_merge_sort(a, middle + 1, right);
	 merge(a, left, middle, right);
  }
}

void merge_sort(vector<int> &a) {
  do_merge_sort(a, 0, a.size()-1);
}

void print(const vector<int> &a) {
  for (int i = 0; i < a.size(); i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  merge_sort(a);
  print(a);
  return 0;
}