DEV Community

Ajith R
Ajith R

Posted on

Merge sort that you want to know

Merge sort is a divide-and-conquer sorting algorithm that divides the input array into smaller subarrays, recursively sorts them, and then merges the sorted subarrays to produce a final sorted array. It is a stable, comparison-based algorithm known for its efficiency and guaranteed O(n log n) time complexity.

Algorithm:

  1. Divide: Divide the unsorted array into two equal halves.
  2. Conquer: Recursively sort each half of the array.
  3. Merge: Merge the sorted halves to produce a single sorted array.

JavaScript Implementation:

function mergeSort(arr) {
    if (arr.length <= 1) {
        return arr;
    }

    const mid = Math.floor(arr.length / 2);
    const leftHalf = mergeSort(arr.slice(0, mid));
    const rightHalf = mergeSort(arr.slice(mid));

    return merge(leftHalf, rightHalf);
}

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

    while (leftIndex < left.length && rightIndex < right.length) {
        if (left[leftIndex] < right[rightIndex]) {
            result.push(left[leftIndex]);
            leftIndex++;
        } else {
            result.push(right[rightIndex]);
            rightIndex++;
        }
    }

    return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
}
Enter fullscreen mode Exit fullscreen mode

Advantages of Merge Sort:

  1. Efficiency: Merge sort guarantees O(n log n) time complexity in all cases, making it one of the most efficient sorting algorithms. It performs well for large datasets and is suitable for general-purpose sorting.

  2. Stability: Merge sort is a stable sorting algorithm, meaning it preserves the relative order of equal elements. This property is useful in certain applications where maintaining the original order is important.

  3. Predictable Performance: Merge sort's time complexity is not affected by the initial order of elements in the array. It consistently achieves optimal performance regardless of input characteristics.

Disadvantages of Merge Sort:

  1. Space Complexity: Merge sort requires additional space proportional to the size of the input array for storing temporary arrays during the merging phase. This can be a drawback for memory-constrained environments or when sorting large arrays.

  2. Not In-Place: Merge sort is not an in-place sorting algorithm, as it requires auxiliary space for merging subarrays. This may lead to increased memory usage, especially for large datasets.

  3. Complexity of Implementation: Implementing merge sort can be more complex compared to simpler sorting algorithms like insertion sort or bubble sort. It involves recursion and multiple subroutines for dividing, conquering, and merging subarrays.

Conclusion:

https://github.com/ajithr116/Data-Structures/tree/main/06-sorting/mergesort is a highly efficient, stable, and predictable sorting algorithm suitable for a wide range of applications. Its guaranteed time complexity of O(n log n) makes it a popular choice for sorting large datasets or when optimal performance is required. While it requires additional space and may be more complex to implement, merge sort's efficiency and stability outweigh these drawbacks in many scenarios.


Top comments (0)