Insertion Sort Algorithm

Insertion Sort Algorithm

In this tutorial, you will learn how insertion sort works. Also, you will find working examples of insertion sort in C, C++, Java and Python.
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?

Suppose we need to sort the following array.
Insertion Sort Steps
  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 elemet.
    Insertion Sort Steps
  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 begining of the array.
    Insertion Sort Steps
  3. In a similar way, place every unsorted element at its correct position.
    Insertion Sort StepsInsertion Sort Steps

Insertion Sort Algorithm

  1. insertionSort(array)
  2. mark first element as sorted
  3. for each unsorted element X
  4. 'extract' the element X
  5. for j <- lastSortedIndex down to 0
  6. if current element j > X
  7. move sorted element to the right by 1
  8. break loop and insert X here
  9. end insertionSort
 
  1. // Insertion sort in C++
  2. #include <iostream>
  3. using namespace std;
  4. void printArray(int array[], int size)
  5. {
  6. for (int i = 0; i < size; i++)
  7. {
  8. cout << array[i] << " ";
  9. }
  10. cout << endl;
  11. }
  12. void insertionSort(int array[], int size)
  13. {
  14. for (int step = 1; step < size; step++)
  15. {
  16. int key = array[step];
  17. int j = step - 1;
  18. while (key < array[j] && j >= 0)
  19. {
  20. // For descending order, change key<array[j] to key>array[j].
  21. array[j + 1] = array[j];
  22. --j;
  23. }
  24. array[j + 1] = key;
  25. }
  26. }
  27. int main()
  28. {
  29. int data[] = {9, 5, 1, 4, 3};
  30. int size = sizeof(data) / sizeof(data[0]);
  31. insertionSort(data, size);
  32. cout << "Sorted array in ascending order:\n";
  33. printArray(data, size);
  34. }
 

Complexity

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

Comments

Popular posts from this blog

C Programming Tutorials

DataStructure and Algorithm

Java Programming Tutorial