#include <iostream>
#include "0-pruebas.h"
using namespace std;
void merge(int a[], int low, int mid, int high){
int n=high-low+1;
// Arreglo temportal b[] donde se guardan
// Los elementos fusionados
int b[n];
b[n]={}; // se inicializa en cero
int left=low;
int right=mid+1;
int bIdx=0;
// Fusion normal: cuando ambas mitades tienen
// elementos que no se han fusionado
while(left<= mid && right<=high){
if(a[left]<= a[right]){
b[bIdx++]=a[left++];
} else{
b[bIdx++]=a[right++];
}
}
// Los elementos que quedan se copian a b[]
while(left<=mid) b[bIdx++]= a[left++];
while(right<=high) b[bIdx++]= a[right++];
// El resultado de la fusion se copia de nuevo a a[]
for(int k = 0; k<n; k++){
a[low+k]=b[k];
}
}
/*Utilizar mergeSort con los elementos
desde a[low] - hasta a[high]*/
void mergeSort(int a[], int low, int high){
if (low < high){
int mid =(low+high)/2;
//Dividir a[] en dos mitades y
//ordenarlas recursivamente
mergeSort(a,low,mid);
mergeSort(a,mid+1,high);
//fusionar las mitades ordenadas
//Se fusiona a[low ... mid] y a[mid+1 ... high]
//en a[low... high]
merge(a,low,mid,high);
}else{
//Caso base: (low >= high)
return;
}
}