Open In App
Related Articles

Merge Sort vs. Insertion Sort

Improve Article
Improve
Save Article
Save
Like Article
Like

Pre-requisite: Merge Sort, Insertion Sort 
Merge Sort: is an external algorithm based on divide and conquer strategy. In this sorting
 

  1. The elements are split into two sub-arrays (n/2) again and again until only one element is left.
  2. Merge sort uses additional storage for sorting the auxiliary array.
  3. Merge sort uses three arrays where two are used for storing each half, and the third external one is used to store the final sorted list by merging the other two and each array is then sorted recursively.
  4. At last, all sub-arrays are merged to make it ‘n’ element size of the array.

Below is the image to illustrate Merge Sort
 

Insertion Sort is a sorting algorithm in which elements are taken from an unsorted item, inserting it in sorted order in front of the other items, and repeating until all items are in order. The algorithm is simple to implement and usually consists of two loops: an outer loop to pick items and an inner loop to iterate through the array. It works on the principle of sorting playing cards in our hands. 
Below is the image to illustrate Insertion Sort
 

Difference between Merge sort and Insertion sort: 
 

  • Time Complexity: In Merge Sort the Worst Case: O(N*log N), Average Case: O(N*log N), and Best Case: O(N*log N)
    whereas 
    In Insertion Sort the Worst Case: O(N2), Average Case: O(N2), and Best Case: O(N).
  • Space Complexity: Merge sort being recursive takes up the auxiliary space complexity of O(N) hence it cannot be preferred over the place where memory is a problem, 
    whereas 
    In Insertion sort only takes O(1) auxiliary space complexity. It sorts the entire array just by using an extra variable.
  • Datasets: Merge Sort is preferred for huge data sets. It happens to compare all the elements present in the array hence is not much helpful for small datasets, 
    whereas 
    Insertion Sort is preferred for fewer elements. It becomes fast when data is already sorted or nearly sorted because it skips the sorted values.
  • Efficiency: Considering average time complexity of both algorithm we can say that Merge Sort is efficient in terms of time and Insertion Sort is efficient in terms of space.
  • Sorting Method: The merge sort is an external sorting method in which the data that is to be sorted cannot be accommodated in the memory and needed auxiliary memory for sorting, 
    whereas 
    Insertion sort is based on the idea that one element from the input elements is consumed in each iteration to find its correct position i.e., the position to which it belongs in a sorted array.
  • Stability: Merge sort is stable as two elements with equal value appear in the same order in sorted output as they were in the input unsorted array, 
    whereas 
    Insertion sort takes O(N2) time on both data structures(Array and Linked list). If the CPU has an efficient memory block move function then the array may be quicker. Otherwise, there probably isn’t that much of a time difference.

MERGE SORT PROGRAM:

C++




#include <iostream>
using namespace std;
 
void merge(int arr[], int l, int m, int r) {
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;
 
    int L[n1], R[n2];
 
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];
 
    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        } else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
 
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
 
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}
 
void mergeSort(int arr[], int l, int r) {
    if (l < r) {
        int m = l+(r-l)/2;
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);
        merge(arr, l, m, r);
    }
}
 
void printArray(int A[], int size) {
    int i;
    for (i=0; i < size; i++)
        cout << A[i] << " ";
    cout << endl;
}
 
int main() {
    int arr[] = {12, 11, 13, 5, 6, 7};
    int arr_size = sizeof(arr)/sizeof(arr[0]);
 
    cout << "Given array is \n";
    printArray(arr, arr_size);
 
    mergeSort(arr, 0, arr_size - 1);
 
    cout << "\nSorted array is \n";
    printArray(arr, arr_size);
    return 0;
}

C




#include <stdio.h>
 
void merge(int arr[], int l, int m, int r) {
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;
 
    int L[n1], R[n2];
 
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];
 
    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        } else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
 
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
 
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}
 
void mergeSort(int arr[], int l, int r) {
    if (l < r) {
        int m = l+(r-l)/2;
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);
        merge(arr, l, m, r);
    }
}
 
void printArray(int A[], int size) {
    int i;
    for (i=0; i < size; i++)
        printf("%d ", A[i]);
    printf("\n");
}
 
int main() {
    int arr[] = {12, 11, 13, 5, 6, 7};
    int arr_size = sizeof(arr)/sizeof(arr[0]);
 
    printf("Given array is \n");
    printArray(arr, arr_size);
 
    mergeSort(arr, 0, arr_size - 1);
 
    printf("\nSorted array is \n");
    printArray(arr, arr_size);
    return 0;
}
//This code is contributed snehalsalokhe

Java




class MergeSort {
  // marge array
    void merge(int arr[], int l, int m, int r) {
        int i, j, k;
        int n1 = m - l + 1;
        int n2 = r - m;
 
        int L[] = new int[n1];
        int R[] = new int[n2];
 
        for (i = 0; i < n1; i++)
            L[i] = arr[l + i];
        for (j = 0; j < n2; j++)
            R[j] = arr[m + 1+ j];
 
        i = 0;
        j = 0;
        k = l;
        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            } else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
 
        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }
 
        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }
    }
    // marge sort devide function
    void mergeSort(int arr[], int l, int r) {
        if (l < r) {
            int m = l + (r - l) / 2;
            mergeSort(arr, l, m);
            mergeSort(arr, m + 1, r);
            merge(arr, l, m, r);
        }
    }
    // Print array
    void printArray(int arr[]) {
        int n = arr.length;
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
    // Drive code
    public static void main(String args[]) {
        int arr[] = {12, 11, 13, 5, 6, 7};
 
        System.out.println("Given array is");
        MergeSort ob = new MergeSort();
        ob.printArray(arr);
 
        ob.mergeSort(arr, 0, arr.length - 1);
 
        System.out.println("\nSorted array is");
        ob.printArray(arr);
    }
}
// This code is contributed by shivhack999

C#




using System;
 
class Program {
    static void Merge(int[] arr, int l, int m, int r) {
        int i, j, k;
        int n1 = m - l + 1;
        int n2 = r - m;
 
        int[] L = new int[n1];
        int[] R = new int[n2];
 
        for (i = 0; i < n1; i++)
            L[i] = arr[l + i];
        for (j = 0; j < n2; j++)
            R[j] = arr[m + 1 + j];
 
        i = 0;
        j = 0;
        k = l;
        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            } else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
 
        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }
 
        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }
    }
 
    static void MergeSort(int[] arr, int l, int r) {
        if (l < r) {
            int m = l + (r - l) / 2;
            MergeSort(arr, l, m);
            MergeSort(arr, m + 1, r);
            Merge(arr, l, m, r);
        }
    }
 
    static void Main(string[] args) {
        int[] arr = new int[] { 12, 11, 13, 5, 6, 7 };
        int arrSize = arr.Length;
 
        Console.WriteLine("Given array is");
        Console.WriteLine(string.Join(" ", arr));
 
        MergeSort(arr, 0, arrSize - 1);
 
        Console.WriteLine("\nSorted array is");
        Console.WriteLine(string.Join(" ", arr));
    }
}

Output

Given array is 
12 11 13 5 6 7 

Sorted array is 
5 6 7 11 12 13 

INSERTION SORT PROGRAM:

C




#include<stdio.h>
 
void insertionSort(int arr[], int n) {
   int i, key, j;
   for (i = 1; i < n; i++) {
       key = arr[i];
       j = i - 1;
       while (j >= 0 && arr[j] > key) {
           arr[j + 1] = arr[j];
           j = j - 1;
       }
       arr[j + 1] = key;
   }
}
 
void printArray(int arr[], int n) {
   int i;
   for (i = 0; i < n; i++)
       printf("%d ", arr[i]);
   printf("\n");
}
 
int main() {
   int arr[] = {12, 11, 13, 5, 6};
   int n = sizeof(arr)/sizeof(arr[0]);
 
   printf("Given array is \n");
   printArray(arr, n);
 
   insertionSort(arr, n);
 
   printf("\nSorted array is \n");
   printArray(arr, n);
 
   return 0;
}

Output

Given array is 
12 11 13 5 6 

Sorted array is 
5 6 11 12 13 

Tabular Representation: 

ParametersMerge SortInsertion Sort
Worst Case ComplexityO(N*log N)O(N2)
Average Case ComplexityO(N*log N)O(N2)
Best Case ComplexityO(N*log N)O(N)
Auxiliary Space ComplexityO(N)O(1)
Works well onOn huge dataset.On small dataset.
EfficiencyComparatively Efficient.Comparatively Inefficient.
Inplace SortingNoYes
Algorithm ParadigmDivide and ConquerIncremental Approach
UsesIt is used for sorting linked list in O(N*log N), for Inversion Count problem, External sorting, etc.It is used when number of elements is small. It can also be useful when input array is almost sorted, only few elements are misplaced in complete big array.

Last Updated : 19 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials