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

114
Views
Merge Sort question: Maximum call stack exceeded, avoided infinite recursion

Unable to get a terminal output because of error: Uncaught RangeError: Maximum call stack size exceeded.

Tried to look at similar questions on stackoverflow, like this Maximum call stack size exceeded in merge sort and added conditionals to possibly stop infinite recursion.

Still couldn't fix the problem. Hopefully looking for an answer.

const unsortedArray = [1, 10, 5, 8, 7, 6, 4, 3, 2, 9];
const sortedArray = Array(unsortedArray.length);

function sort(unsortedArray, start, middle, end) {

    let i= start;
    let j= middle;
    let k= start;

    while(i < middle && j <= end) {

        if(unsortedArray[i]<=unsortedArray[j]) {
            sortedArray[k] = unsortedArray[i];
            i++
        } else {
            sortedArray[k] = unsortedArray[j];
            j++;
        }
        k++
    }

    if (i >= middle) {
        while(j<= end) {
            sortedArray[k] = unsortedArray[j];
            j++;
            k++;
        }
    } else if (j >= end) {
        while (i <= middle) {
            sortedArray[k] = unsortedArray[i];
            i++;
            k++;
        }
    }
}

function splitMerge(unsortedArray, start, end) {
    if(end>start) {
        middle = Math.floor((start + end)/2);
        splitMerge(unsortedArray, start, middle);
        splitMerge(unsortedArray, middle, end);
        sort(unsortedArray, start , middle, end);
    }
    return sortedArray;
}

const showResult = splitMerge(unsortedArray, 0, unsortedArray.length-1);
console.log(showResult);
about 4 years ago · Juan Pablo Isaza
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!