JavaScript: Merge Sort Algorithm
By: Tina Luk
Tags: JavaScript, Computer Science, Recursion, Algorithms, Problem Solving

What is sorting?

Sorting is rearranging a set of data into a specific order such as ascending to descending for numbers, or vice versa. A sorting algorithm takes in a given array or list and outputs a sorted array or list.

Sorting is applied everywhere. When we are on a website and sorting listings by lowest price to highest, ratings, etc. or searching for a book at the library. The concept of sorting makes it easier to arrange sets of data, therefore making it easier to search.

Types of Sorting Algorithms

There are many types of sorting algorithms. Sorting techniques are mainly differentiated by time complexity and space complexity. As the name implies, time complexity is the amount of time an algorithm takes to run as a function of the input length and space complexity is the amount of space or memory required as a function of input length. Below lists a a few different types of sorting algorithms:

Quick sort

Bubble sort

Merge sort

Insertion sort

Heap sort

What is merge sort?

This article covers merge sort. Merge sort is a sorting technique that is based on divide and conquer. The worst case time complexity is O(n log(n)) and space complexity is O(n).

Merge sort is a very popular sorting algorithm and uses a recursive approach.

How does merge sort work?

The following recursive steps take place for merge sort to work:

Divide given array of values in half Continue to divide until reached base case of a single value Merge the two halves in a sorted manner by comparing the values. Pop off the call stack with the sorted half and continue to divide, merge and sort.

Below shows a diagram and steps of an example input using merge sort.

Steps 1, 2, 3 we are dividing the array until we hit the base case. The base case is when we reach a point we can no longer divide the values of the array. There is only a single value in the array. Same with steps 5, 6, 7 we are recursively dividing the array until we hit our base case and pop off the call stack. Step 8 merges the left and right halves are merged in a sorted manner, once merged we pop this returned value off the call stack and this is our right half for step 9. Step 9 does the same merge the left and right halves in a sorted manner. Step 10 now repeats the same recursive process again.

What does this algorithm look like?

const mergeSort = function (array) {

if (array.length <= 1) {

return array;

}

let mid = Math.floor(array.length / 2);

let left = mergeSort(array.slice(0, mid));

let right = mergeSort(array.slice(mid));

return merge(left, right);

};

The merge sort function accepts an array of values. This array is divided recursively until it hits the base case of length of 1. Once these sub arrays are returned and popped off the call stack the function continues and merges the right and left sub array. Inside the merge function it will return an a merge sorted array. Every time the merge function is used, the left and right sub arrays are already sorted. Therefore we are merging sorted arrays.

Below if the merge function.

const merge = function (left, right) {

let sorted = []; while (left.length > 0 && right.length > 0) {

if (left[0] < right[0]) {

sorted.push(left.shift());

} else {

sorted.push(right.shift());

}

} if (left.length > 0) {

sorted = sorted.concat(left);

}

if (right.length > 0) {

sorted = sorted.concat(right);

}

return sorted;

};

The merge function accepts 2 sorted arrays. The function will merge these sorted arrays with a simple while loop comparing the first values in each array and popping them off and adding it into the new sorted array.