Introduction to Merge Sort

Hello, rookie coders!

Today, we are focusing on the mighty Merge Sort, a potent way to organize data. It's time to explore and code this technique in JavaScript. Imagine shuffling a deck of cards and then rearranging them in order. Merge Sort accomplishes exactly this with data, but on a grander scale, making it top-tier for large-scale data.

Understanding the Merge Process in JavaScript

First, let's build a merge() function in JavaScript. It merges two sorted arrays into a single sorted array. Think of it as combining two sorted stacks of cards into one sorted stack.

function merge(left, right) {
    let resultArray = [], leftIndex = 0, rightIndex = 0;

    // sorting and merging process
    while(leftIndex < left.length && rightIndex < right.length) {
        if(left[leftIndex] < right[rightIndex]) {
            resultArray.push(left[leftIndex]); // the smaller of the two elements is added to the result array
            leftIndex++; // move to the next element in the left array
        } else {
            resultArray.push(right[rightIndex]); // same as above but with the right array
            rightIndex++;
        }
    }

    // Return the sorted array
    return resultArray
        .concat(left.slice(leftIndex))
        .concat(right.slice(rightIndex));
}

The merge() function above takes two sorted arrays (left and right) and combines them into one sorted array (resultArray).

Seemingly tricky, the code is very straightforward:

  1. We place two pointers, leftIndex and rightIndex, at the beginning of the left and right arrays.
  2. We choose the smaller element, put it in the final array resultArray, and move the corresponding pointer further.
  3. We keep doing this until one of the pointers reaches the end of its array.

We stop the process when one of the pointers reaches the end of its array, but some elements could be left in the other array.

To handle this, we copy the remaining elements of both arrays (if any) to the end of the resulting arr array, using .concat method.

Implementing Merge Sort using JavaScript

Next, we'll implement the complete Merge Sort algorithm in JavaScript. This process involves splitting an array into halves until we reach an array containing only one element. Arrays of one element are naturally sorted so that we can merge them into one sorted array. Then, we can merge the obtained arrays of two elements. This process goes until we merge all arrays into one sorted array.

function mergeSort (unsortedArray) {
    if (unsortedArray.length <= 1) {
        return unsortedArray; // If the array has only one element, it's already sorted
    }
    
    const middle = Math.floor(unsortedArray.length / 2); // This will get the midpoint of the array
    const left = unsortedArray.slice(0, middle); // We split the array into two halves
    const right = unsortedArray.slice(middle); // This is the right half

    // Merge the two halves back together
    return merge(
        mergeSort(left), mergeSort(right)
    );
}

Voila! You've successfully decoded the Merge Sort algorithm in JavaScript!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal