Back to Course


0% Complete
0/82 Steps
  1. Getting Started with Algorithm
    What is an Algorithm?
  2. Characteristics of Algorithm
    1 Topic
  3. Analysis Framework
  4. Performance Analysis
    3 Topics
  5. Mathematical Analysis
    2 Topics
  6. Sorting Algorithm
    Sorting Algorithm
    10 Topics
  7. Searching Algorithm
    6 Topics
  8. Fundamental of Data Structures
  9. Queues
  10. Graphs
  11. Trees
  12. Sets
  13. Dictionaries
  14. Divide and Conquer
    General Method
  15. Binary Search
  16. Recurrence Equation for Divide and Conquer
  17. Finding the Maximum and Minimum
  18. Merge Sort
  19. Quick Sort
  20. Stassen’s Matrix Multiplication
  21. Advantages and Disadvantages of Divide and Conquer
  22. Decrease and Conquer
    Insertion Sort
  23. Topological Sort
  24. Greedy Method
    General Method
  25. Coin Change Problem
  26. Knapsack Problem
  27. Job Sequencing with Deadlines
  28. Minimum Cost Spanning Trees
    2 Topics
  29. Single Source Shortest Paths
    1 Topic
  30. Optimal Tree Problem
    1 Topic
  31. Transform and Conquer Approach
    1 Topic
  32. Dynamic Programming
    General Method with Examples
  33. Multistage Graphs
  34. Transitive Closure
    1 Topic
  35. All Pairs Shortest Paths
    6 Topics
  36. Backtracking
    General Method
  37. N-Queens Problem
  38. Sum of Subsets problem
  39. Graph Coloring
  40. Hamiltonian Cycles
  41. Branch and Bound
    2 Topics
  42. 0/1 Knapsack problem
    2 Topics
  43. NP-Complete and NP-Hard Problems
    1 Topic
Lesson 6, Topic 3
In Progress

Insertion Sort

Lesson Progress
0% Complete

Insertion sort works in the similar way as we sort cards in our hand in a card game.

We assume that the first card is already sorted then, we select an unsorted card. If the unsorted card is greater than the card in hand, it is placed on the right otherwise, to the left. In the same way, other unsorted cards are taken and put at their right place.

A similar approach is used by insertion sort.

Insertion sort is a sorting algorithm that places an unsorted element at its suitable place in each iteration.

How Insertion Sort Works?

KodNest 1

Suppose we need to sort the following array.

  1. The first element in the array is assumed to be sorted. Take the second element and store it separately in key. Compare key with the first element. If the first element is greater than key, then key is placed in front of the first element.
KodNest Insertion sort 0 1 2

2. Now, the first two elements are sorted.

Take the third element and compare it with the elements on the left of it. Placed it just behind the element smaller than it. If there is no element smaller than it, then place it at the beginning of the array.

KodNest Insertion sort 1 1

3. In a similar way, place every unsorted element at its correct position.

KodNest Insertion sort 2 2
KodNest Insertion sort 3 2

Insertion Sort Algorithm

  mark first element as sorted
  for each unsorted element X
    'extract' the element X
    for j <- lastSortedIndex down to 0
      if current element j > X
        move sorted element to the right by 1
    break loop and insert X here
end insertionSort

Insertion Sort Program in C

// Insertion sort in C
    #include <stdio.h>
    void printArray(int array[], int size)
      for (int i = 0; i < size; i++)
        printf("%d ", array[i]);
    void insertionSort(int array[], int size)
      for (int step = 1; step < size; step++)
        int key = array[step];
        int j = step - 1;
        while (key < array[j] && j >= 0)
          // For descending order, change key<array[j] to key>array[j].
          array[j + 1] = array[j];
        array[j + 1] = key;
    int main()
      int data[] = {9, 5, 1, 4, 3};
      int size = sizeof(data) / sizeof(data[0]);
      insertionSort(data, size);
      printf("Sorted array in ascending order:\n");
      printArray(data, size);


Time Complexities

  • Worst Case Complexity: O(n2)

    Suppose, an array is in ascending order, and you want to sort it in descending order. In this case, worse case complexity occers.

    Each element has to be compared with each of the other elements so, for every nth element, (n-1) number of comparisons are made.

    Thus, the total number of comparisons = n*(n-1) ~ n2
  • Best Case Complexity: O(n)

    When the array is already sorted, the outer loop runs for n number of times whereas the inner loop does not run at all. So, there is only n number of comparison. Thus, complexity is linear.
  • Average Case Complexity: O(n2)

    It occurs when the elements of a array are in jumbled order (neither ascending nor descending).

Space Complexity

Space complexity is O(1) because an extra variable key is used.

Insertion Sort Applications

The insertion sort is used when:

  • the array is has a small number of elements
  • there are only a few elements left to be sorted

New Report