Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

82
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda