Merge sort is often considered the ultimate sorting algorithm due to its optimization and reliability. While it may be slightly unintuitive, its logic is elegant, and its efficiency makes it a must-know for every developer.
With just a few lines of code, merge sort can consistently sort any array quickly, even with large datasets. It is one of the fastest algorithms, with a time complexity of O(n log n), and it supports other fast algorithms like Timsort.
Merge sort involves a recursive function, which can be unintuitive for some. If you're not familiar with recursion, you might want to refer to my guide on recursion here.
Merge sort follows the divide and conquer strategy, consisting of two main steps: dividing and merging.
The first step is to divide the array into subarrays until each subarray has only one element. This makes comparison straightforward and limits the number of iterations.
Once the array is divided into subarrays of one element, we merge them while comparing the elements to ensure the resulting array is sorted.
To make the code cleaner and easier to understand, we'll break the algorithm into two functions: merge and mergeSort.
function merge(arr1, arr2) {
let new_arr= [];
let i = 0;
let j = 0;
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
new_arr.push(arr1[i]);
i++;
} else {
new_arr.push(arr2[j]);
j++;
}
}
if (i < arr1.length) {
new_arr.push(...arr1.slice(i));
}
if (j < arr2.length) {
new_arr.push(...arr2.slice(j));
}
return new_arr;
}
function mergeSort(arr) {
if (arr.length <= 1) return arr;
const mid = Math.floor(arr.length / 2);
const left = mergeSort(arr.slice(0, mid));
const right = mergeSort(arr.slice(mid));
return merge(left, right);
}
Note that a recursive function calls itself until reaching the base case. In this case, we keep dividing the array in half until each subarray has only one element.
To go into a little more detail, let's walk through the recursion step by step:
To illustrate this with an example, consider an array 3, 1, 4, 2:
Merge sort is one of the fastest sorting algorithms, suitable for any dataset size. Here are some key points to consider:
Merge sort is suitable for most cases, but some algorithms may perform better in specific scenarios. For example, quicksort is often faster in practice, but merge sort is more consistent and stable.
Merge sort is a powerful and efficient sorting algorithm, especially for large datasets. While it may be challenging to understand initially, its performance is almost unmatched, with a time complexity of O(n log n) in all cases.
Thank you for your attention. I hope you learned something new. Writing this content also helps me clarify my understanding.
Happy coding!
Honestly i am no one, i've just been coding for 3 years now and i like to document every solutions to every problem i encounter. Extracting as much code snippets and tutorials i can so that if i ever need it again i just have to pop back here and get a quick refresher.
Feel free to me through this learning journey by providing any feedback and if you wanna support me: