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

Envío 1517

Problema 0xde - Ordenar un arreglo grande

  • Autor: d4vsanchez
  • Fecha: 2020-11-02 16:35:15 UTC (Hace alrededor de 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.006 s 1 KBi
#2
Correcto
0.007 s 1 KBi
#3
Correcto
0.006 s 20 KBi
#4
Correcto
0.006 s 1 KBi
#5
Correcto
0.005 s 1 KBi
#6
Correcto
0.005 s 1 KBi
#7
Correcto
0.082 s 2 KBi
#8
Correcto
0.09 s 2 KBi
#9
Correcto
0.089 s 2 KBi
#10
Correcto
0.099 s 2 KBi
#11
Correcto
0.112 s 3 KBi
#12
Correcto
0.128 s 3 KBi
#13
Correcto
0.109 s 3 KBi
#14
Correcto
0.099 s 2 KBi
#15
Correcto
0.098 s 3 KBi
#16
Correcto
0.124 s 3 KBi
#17
Correcto
0.135 s 3 KBi
#18
Correcto
0.097 s 2 KBi
#19
Correcto
0.104 s 3 KBi
#20
Correcto
0.089 s 2 KBi
#21
Correcto
0.094 s 2 KBi
#22
Correcto
0.099 s 3 KBi
#23
Correcto
0.098 s 3 KBi
#24
Correcto
0.102 s 3 KBi
#25
Correcto
0.098 s 3 KBi
#26
Correcto
0.112 s 3 KBi
#27
Correcto
0.112 s 3 KBi
Puntos totales: 100 / 100

Código

#include <iostream>
#include <vector>

using namespace std;

void merge_sort(vector<int> &arr) {
  size_t size = arr.size();
  if (size > 1) {
    int middle = size / 2;
    vector<int> left_arr(middle);
    vector<int> right_arr(size - middle);

    for (int i = 0; i < middle; i++) {
      left_arr[i] = arr[i];
    }

    for (int i = 0; i + middle < size; i++) {
      right_arr[i] = arr[i + middle];
    }

    merge_sort(left_arr);
    merge_sort(right_arr);

    int l = 0;
    int r = 0;
    int i = 0;

    size_t left_arr_size = left_arr.size();
    size_t right_arr_size = right_arr.size();

    while (l < left_arr_size and r < right_arr_size) {
      if (left_arr[l] < right_arr[r]) {
        arr[i] = left_arr[l];
        l += 1;
      } else {
        arr[i] = right_arr[r];
        r += 1;
      }
      i += 1;
    }

    while (l < left_arr_size) {
      arr[i] = left_arr[l];
      l += 1;
      i += 1;
    }

    while (r < right_arr_size) {
      arr[i] = right_arr[r];
      r += 1;
      i += 1;
    }
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  merge_sort(arr);
  for (int i = 0; i < arr.size(); i++) {
    cout << arr[i] << " ";
  }
  cout << endl;
  return 0;
}