Merge Sort

on September 16, 2021 · 4 mins read

Introduction to Merge Sort

Merge sort is a classic sorting algorithm that is widely used in computer science. It is an efficient, general-purpose, comparison-based sorting algorithm. It is a divide and conquer algorithm that was invented by John von Neumann in 1945.

Merge sort is a recursive algorithm that continually splits a list in half. If the list is empty or has one item, it is sorted by definition (the base case). If the list has more than one item, we split the list and recursively invoke a merge sort on both halves. Once the two halves are sorted, the fundamental operation, called a merge, is performed. Merging is the process of taking two smaller sorted lists and combining them together into a single, sorted, new list.

In this blog post, we will discuss the basics of the merge sort algorithm, its implementation, and its advantages and disadvantages.

What is Merge Sort?

Merge sort is a sorting algorithm that works by dividing a list of items into two halves, sorting each half, and then merging the two sorted halves into one sorted list. The merge sort algorithm can be used to sort a list of items in either ascending or descending order.

The merge sort algorithm is based on the divide-and-conquer principle. It works by recursively dividing the list into two halves, sorting each half, and then merging the two sorted halves into one sorted list.

How Does Merge Sort Work?

The merge sort algorithm works by recursively dividing the list into two halves, sorting each half, and then merging the two sorted halves into one sorted list.

The merge sort algorithm can be broken down into three main steps:

  1. Divide: Divide the list into two halves.
  2. Conquer: Sort each half of the list.
  3. Merge: Merge the two sorted halves into one sorted list.

The following diagram illustrates how the merge sort algorithm works:

Merge Sort Diagram

Pseudocode

The following pseudocode describes the merge sort algorithm:

MergeSort(A, p, r): 
    if p < r: 
        q = floor((p + r) / 2) 
        MergeSort(A, p, q) 
        MergeSort(A, q + 1, r) 
        Merge(A, p, q, r) 

Merge(A, p, q, r): 
    n1 = q - p + 1 
    n2 = r - q 
    L[1..n1 + 1] 
    R[1..n2 + 1] 
    for i = 1 to n1 
        L[i] = A[p + i - 1] 
    for j = 1 to n2 
        R[j] = A[q + j] 
    L[n1 + 1] = ∞ 
    R[n2 + 1] = ∞ 
    i = 1 
    j = 1 
    for k = p to r 
        if L[i] ≤ R[j] 
            A[k] = L[i] 
            i = i + 1 
        else 
            A[k] = R[j] 
            j = j + 1 

Advantages and Disadvantages

Merge sort is a stable sorting algorithm, meaning that the relative order of elements with equal values is preserved. It is also a very efficient algorithm, with a time complexity of O(n log n).

The main disadvantage of merge sort is that it requires extra space for the temporary arrays. This can be a problem if the list is large and there is limited memory available.

Conclusion

Merge sort is a classic sorting algorithm that is widely used in computer science. It is an efficient, general-purpose, comparison-based sorting algorithm. It is a divide and conquer algorithm that works by recursively dividing the list into two halves, sorting each half, and then merging the two sorted halves into one sorted list.

Merge sort is a stable sorting algorithm with a time complexity of O(n log n). The main disadvantage of merge sort is that it requires extra space for the temporary arrays.

Overall, merge sort is a powerful sorting algorithm that can be used to efficiently sort a list of items.