Using merge sort to recursive sort an array JavaScript


In this problem statement, our aim is to sort an array recursively using merge sort and implement the code with the help of Javascript. So below we will discuss Merge sort and its implementation.

Understanding the problem statement

The problem statement is to write a function for merge sort in Javascript that will help to sort the given input array into an ascending or descending order. And we have to create a function which will sort the elements recursively.

What is the recursive sorting technique?

Recursion is a technique in which a function calls itself to solve a problem. When a function calls itself then it creates a new instance of itself on the stack and the function continues to execute until a required result is not found. This result is the condition that stops the recursion and allows the function to give the result.

So the recursion is a powerful technique which can simplify complex problems and create code more simple and concise. So it is important to use recursion carefully because it leads to stack overflow errors if it is not implemented correctly.

What is a merge sort Algorithm?

In the merge sort, the algorithm sorts an array recursively by breaking it down into smaller subarrays until each subarray consists of one element in it. Then the algorithm combines the adjacent pairs of subarrays into larger and sorted subarrays. This process runs recursively until the whole element of the array is not sorted.

The recursive step involves calling the merge sort function on the left and right parts of the array. These parts are themselves arrays. This call sorts every half part of the array by breaking it down into smaller subarrays. Once it separates all elements the algorithm starts merging the sorted subarrays back together in order to create the resultant sorted array.

Logic for the given problem

For the code we will create a function to do the merge sort. And inside the function we will divide an array into smaller subarrays, sorting those subarrays recursively and then merge them back together in order to produce a fully sorted array. The key operation of the function is the merge step where two sorted subarrays are combined into a single sorted array.

Algorithm

Step 1 − Declare a function called mergeSort which is using a parameter of array.

Step 2 − Inside the function, we will check if the length of the array is 1 or less than 1 its already sorted so just return the array.

Step 3 − Otherwise, we will divide the array into two half arrays left and right and recursively sort every half part using mergeSort.

Step 4 − And once the recursive calls return we will merge the sorted left and right parts together using the merge function.

Step 5 − The merge function will take two sorted arrays left and right and merge them into a single sorted array as the result.

Code for the algorithm

//function to sort the given array
function mergeSort(arr) {
   if (arr.length <= 1) {
      return arr;
   }
   const mid = Math.floor(arr.length / 2);
   const left = arr.slice(0, mid);
   const right = arr.slice(mid);
   return merge(mergeSort(left), mergeSort(right));
}
 
//function to merge the left and right elements
function merge(left, right) {
   const result = [];
    
   while (left.length && right.length) {
      if (left[0] < right[0]) {
         result.push(left.shift());
      } else {
         result.push(right.shift());
      }
   }
    
   return [...result, ...left, ...right];
}
const arr = [4, 1, 5, 2, 6, 3, 7, 8];
console.log(mergeSort(arr));

Complexity

The time taken by the mergeSort function is O(n log n) because we have implemented merge sort which takes O(n log n) time to sort the items. And n is the size of the given array. And the space used by the code is also O(n) as it is storing the result as a sorted array. So this technique makes it an efficient sorting algorithm for large datasets.

Conclusion

So the above created function can be used to sort the given array of size n with time complexity O(n log n). This is a reliable and efficient sorting algorithm.

Updated on: 18-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements