Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

81
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!