Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
merge_sort: Uncaught RangeError: Maximum call stack size exceeded

i am working on a sorting visualizer and i am trying to visualize the merge sorting algo but whenever i call the mergeSort() function, i always get a "maximum call stack size exceeded" error. i have tried to rectify the problem but to no avail.

document.querySelector(".merge").addEventListener("click", mergeSort);
var el = document.querySelectorAll(".bar");
var low = 0;
var high = el.length - 1

function mergeSort(){
  if (low >= high){
    return;
  }
  var mid = parseInt((low + high)/2);
  mergeSort(el, low, mid);
  mergeSort(el, mid + 1, high);
  merge(el, low, mid, high);
}

function merge(arr, low, mid, high){
  var n = mid - low + 1;
  var m = high - mid;

  var leftArray = new Array(n);
  var rightArray = new Array(m);

  for(var i = 0; i < n; i++){
    leftArray[i] = arr[low + i];
  } 
  for(var j = 0; j < m; j++){                    
    rightArray[j] = arr[mid + 1 + j];
  }

  var i = 0;
  var j = 0; 
  var k = low;

  while (i < n && j < m){
    if (leftArray[i].offsetHeight <= rightArray[j].offsetHeight){
      arr[k].offsetHeight = leftArray[i].offsetHeight
      i++;
    }else{
      arr[k].offsetHeight = rightArray[j].offsetHeight;
      j++;
    }
    k++;
  }
  while (i < n){
      arr[k].offsetHeight = leftArray[i].offsetHeight
      i++;
      k++;
  }
  while (j < m){
      arr[k].offsetHeight = rightArray[j].offsetHeight;
      j++;
      k++;
  }
}

                        
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should define el, low and high as arguments in the function mergeSort:

function mergeSort(el, low, high) {
    ...
}

And you should call the function this way:

mergeSort(el, 0, el.length - 1);

The middle index can be computed with integer arithmetics:

var mid = low + ((high - low) >> 1);

Finally the merge function does not sort the array el, it modifies the offsetHeight properties of the array elements, which may or may not be relevant.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda