# Maximum call stack size exceeded error with merge sort function

**URL:** <https://discourse.processing.org/t/maximum-call-stack-size-exceeded-error-with-merge-sort-function/28913>\
**Category:** Coding Questions\
**Created:** [March 27, 2021, 8:33pm UTC](https://discourse.processing.org/t/maximum-call-stack-size-exceeded-error-with-merge-sort-function/28913 "2021-03-27T20:33:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelberge](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@michaelberge](https://discourse.processing.org/u/michaelberge)\
**Post date:** [March 27, 2021, 8:33pm UTC](https://discourse.processing.org/t/maximum-call-stack-size-exceeded-error-with-merge-sort-function/28913/1 "2021-03-27T20:33:07Z")

</div>

Here is the function, it calls ‘Uncaught RangeError: Maximum call stack size exceeded’ when run, I’m not sure why this is happening. I’m thinking somewhere it’s getting caught in an infinite loop:

```auto
function mergeSort(vals) {
  if (vals.length > 1) {
    let mid = floor(vals.length / 2);
    let left = [];
    let right = [];
    
    for (let i = 0; i <= mid; i++) {
      left.push(vals[i]);
    }
    for (let i = mid+1; i < vals.length; i++) {
      right.push(vals[i]);
    }

    mergeSort(left);
    mergeSort(right);
    
    // Merge
    let i, j, k = 0;

    while (i < left.length && j < right.length) {
      if (left[i] < right[j]) {
        values[k] = left[i];
        i++;
      }
      else {
        values[k] = right[j];
        j++;
      }
      k++;
    }

    while (i < left.length) {
      values[k] = left[i];
      i++;
      k++;
    }

    while (j < right.length) {
      values[k] = right[j];
      j++;
      k++;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [March 27, 2021, 9:14pm UTC](https://discourse.processing.org/t/maximum-call-stack-size-exceeded-error-with-merge-sort-function/28913/2 "2021-03-27T21:14:42Z")

</div>

I haven’t run the code so just guessing (it would be helpful if you can provide the complete code with what array you are using etc so we can actually run it) - I see that in `left` you include `mid`. This is problematic if you think about inputting an array of 2 elements, both end up in `left` and nothing in `right` causing infinite recursion.

I think the guideline on this forum shows you some tips on debugging. In this case you can try to `console.log(vals)` to see what’s actually happening and also use simple examples (what if the array length is only 1, what if 2,…) to narrow down the problem.

---

<div class="post-metadata">

**Author:** ![michaelberge](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@michaelberge](https://discourse.processing.org/u/michaelberge)\
**Post date:** [March 27, 2021, 9:42pm UTC](https://discourse.processing.org/t/maximum-call-stack-size-exceeded-error-with-merge-sort-function/28913/3 "2021-03-27T21:42:20Z")

</div>

I fixed the case with the left including mid and it now runs but doesn’t do anything. Guess I’ll go from here thanks for the help!

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 27, 2021, 10:13pm UTC](https://discourse.processing.org/t/maximum-call-stack-size-exceeded-error-with-merge-sort-function/28913/4 "2021-03-27T22:13:14Z")

</div>

Where is this variable `values` initialized?:

```auto
        values[k] = left[i];

```

_EDIT (March 28, 2021):_

Perhaps it is a global variable that is being used within the function, which means that the function is not self-contained. Consequently, we need to have the entire surrounding context of the function available to us in order to reproduce whatever problems you are having.

---

<div class="post-metadata">

**Author:** ![michaelberge](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@michaelberge](https://discourse.processing.org/u/michaelberge)\
**Post date:** [March 29, 2021, 12:50am UTC](https://discourse.processing.org/t/maximum-call-stack-size-exceeded-error-with-merge-sort-function/28913/5 "2021-03-29T00:50:04Z")

</div>

It’s a global variable containing the unsorted values.

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 29, 2021, 5:13am UTC](https://discourse.processing.org/t/maximum-call-stack-size-exceeded-error-with-merge-sort-function/28913/6 "2021-03-29T05:13:34Z")

</div>

Because the function receives the data via a parameter and also operates on that data via an external variable, it is difficult to keep track of what the function is doing.

_EDIT (March 29, 2021):_

> [@micuat](#):
>
> it would be helpful if you can provide the complete code with what array you are using etc so we can actually run it

@michaelberge, if you can provide us with that code, we would be in a better position to understand the relationship between the function and the data, and to diagnose the problem.
