### Merge Sort Algorithm in JavaScript

In this article, we will discuss working and implementation of Merge Sort algorithm in Java.
Merge sort is an example of the divide and conquer strategy. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
It works on the below principle:
• Divide the list into sub list of about half size in each iteration until each sublist has only one element.
• Merge each sublist repeatedly to create a sorted list. It will run until we have only 1 sorted list. This will be the sorted list.

# How the Merge Sort Works?

To understand merge sort, we take an unsorted array like the following −
We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size 4.
This does not change the sequence of appearance of items in the original. Now we divide these two arrays into halves.
We further divide these arrays and we achieve an atomic value which can no more be divided.
Now, we combine them in exactly the same manner as they were broken down. Please note the color codes given to these lists.
We first compare the element for each list and then combine them into another list in a sorted manner. We see that 20 and 23 are in sorted positions. We compare 25 and 21 and in the target list of 2 values, we put 21 first, followed by 25. Similarly, compare 30 and 10 these are not sorted so let's sort it and compare 65 and 15 these are also not sorted so sort it out. The final outcome is:
In the next iteration of the combining phase, we compare lists of two data values and merge them into a list of found data values placing all in sorted order. Note that the items should be sorted while combining these halves.
After the final merging, the list should look like this −

### 10 15 20 21 23 25 30 65

Let's implement Merge sort algorithm using JavaScript.

# Merge Sort Algorithm in JavaScript

```function mergeSort(array, compareFn = defaultCompare) {
if (array.length > 1) {
const {
length
} = array;
const middle = Math.floor(length / 2);
const left = mergeSort(array.slice(0, middle), compareFn);
const right = mergeSort(array.slice(middle, length), compareFn);
array = merge(left, right, compareFn);
}
return array;
}

const Compare = {
LESS_THAN: -1,
BIGGER_THAN: 1,
EQUALS: 0
};

function merge(left, right, compareFn) {
let i = 0;
let j = 0;
const result = [];
while (i < left.length && j < right.length) {
result.push(compareFn(left[i], right[j]) === Compare.LESS_THAN ? left[i++] : right[j++]);
}
return result.concat(i < left.length ? left.slice(i) : right.slice(j));
}

function defaultCompare(a, b) {
if (a === b) {
return Compare.EQUALS;
}
return a < b ? Compare.LESS_THAN : Compare.BIGGER_THAN;
}
```

## Using Merge Sort Algorithm

To test the selection sort algorithm, we can use the following code:
```const array1 = [20, 23, 25, 21, 30, 10, 65, 15];
const array2 = mergeSort(array1);
console.log(array2);```

## Output

``[10, 15, 20, 21, 23, 25, 30, 65]``