MergeSort in Javascrtipt!

Faizah Ahsan
2 min readJan 10, 2021

Sorting refers to arranging items of a list in a specific order (numerical or alphabetic). Sorting is generally used in tandem with searching. It is generally easier to search for an element (called the key) in a given list if the list is sorted, both visually and algorithmically. There are many ways (algorithms) to sort a given list of elements. Merge Sort is one of the more popular, and more efficient ways to do so.

Before we jump into mergeSort lets talk about how to merge first. Below is an example:

Merge Sort is usually solved with a recursive solution, and that the recursive solution used two separate functions. Each of them has a different purpose within the context of the algorithm. Merge sort uses the concept of divide-and-conquer to sort the given list of elements. It breaks down the problem into smaller subproblems until they become simple enough to solve directly.

example:

[97, 0, 22, -30]

Here are the steps Merge Sort takes:

  1. Split the array into two then split those halves.
  2. Continue dividing the subarrays in the same manner until you are left with only single element arrays.
  3. Starting with the single element arrays, merge the subarrays so that each merged subarray is sorted.
  4. Repeat step 3 unit with end up with a single sorted array.

Here is the implementation:

--

--

Faizah Ahsan

Junior Full-Stack Web Developer. Looking for opportunities!