# Merge Sort

![](/files/SLNOeL5OpKpOPPA7f8EH)

* nlogn, n

```jsx
function mergeSort(arr) {
  return mergeSortHelper(arr, 0, arr.length - 1);
}

function mergeSortHelper(arr, s, e) {
  if (s >= e) {
    return [arr[s]];
  }
  
  const m = s + Math.floor((e - s) / 2);

  const arr1 = mergeSortHelper(arr, s, m);
  const arr2 = mergeSortHelper(arr, m + 1, e);

  return merge(arr1, arr2);
}

function merge(arr1, arr2) {
  const merged = new Array(arr1.length + arr2.length);
  let currIdx = 0;
  
  let idx1 = 0;
  let idx2 = 0;
  while (idx1 < arr1.length && idx2 < arr2.length) {
    const v1 = arr1[idx1];
    const v2 = arr2[idx2];

    if (v1 <= v2) {
      merged[currIdx] = v1;
      idx1++;
    } else {
      merged[currIdx] = v2;
      idx2++;
    }

    currIdx++;
  }

  while (idx1 < arr1.length) {
    merged[currIdx] = arr1[idx1];
    idx1++;
    currIdx++;
  }

  while (idx2 < arr2.length) {
    merged[currIdx] = arr2[idx2];
    idx2++;
    currIdx++;
  }

  return merged;
}

// Do not edit the line below.
exports.mergeSort = mergeSort;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://algorithm.prettylog.com/algorithm-problems/algoexpert/hardest/merge-sort.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
