"Insertion sort algorithm" is one of the simplest and most commonly used sorting algorithms when it comes to the ordering of a deck of cards in everyday life. You insert each element into its proper place in the sorted array using this algorithm. Despite its simplicity, Insertion sort is quite inefficient compared to its allies such as quicksort, and merge sort to name a few.
By the end of this tutorial, you will have a better understanding of the fundamental technicalities of the Insertion sort with all the necessary details along with practical implementations.
What Is the Insertion Sort Algorithm?
Insertion sort algorithm is a basic sorting algorithm that sequentially sorts each item in the final sorted array or list. It is significantly low on efficiency while working on comparatively larger data sets. While other algorithms such as quicksort, heapsort, or merge sort have time and again proven to be far more effective and efficient.
Now, have a look at the working of the Insertion sort algorithm to get a better understanding of the insertion sort.
How Does Insertion Sort Work?
To sort an array in ascending order, you will follow the steps below:
- Iterate through the array from arr[1] to arr[n].
- Compare the current element (key) to one that came before it.
- If the data at the current index is less than the data at the previous index, you will compare it to the element before it.
- You will shift bigger elements to the next index to make space for swapped elements, and then you will iterate the same steps again to sort the complete array.
And this is how the Insertion sort algorithm works. Now implement this algorithm through a simple C++ code.
How to Implement the Insertion Sort Algorithm?
You will be provided with a one-dimensional array of elements {6, 5, 4, 2, 3}. You have to write a code to sort this array using the insertion sort algorithm. The final array should come out to be as {2, 3, 4, 5, 6}.
Code:
//A C++ program to sort an array using insertion sort
#include <bits/stdc++.h>
using namespace std;
//A Function to sort an array using insertion sort
void insertion_sort(int array[], int size)
{
int i, key, k;
for (i = 1; i < size; i++)
{
key = array[i];
k = i - 1;
// Move Ar[0..i-1] elements,
//which are larger than the key,
//to one place over their present location
while (k >= 0 && array[k] > key)
{
array[k + 1] = array[k];
k = k - 1;
}
array[k + 1] = key;
}
}
// A function to print the array of size n
void print_array(int array[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
/* Driver code */
int main()
{
int arr[] = { 6, 5, 4, 2, 3 };
int size = sizeof(arr) / sizeof(arr[0]);
cout<<"Array before sorting:\n";
print_array(arr, size);
insertion_sort(arr, size);
cout<<"\nArray after sorting:\n";
print_array(arr, size);
return 0;
}
You have now explored the working of insertion sort with a code. Now, look at some of the advantages of the Insertion sort.
What Are the Advantages of the Insertion Sort?
You will now look at a few major benefits of using insertion sort and a few scenarios where insertion sort is proven to be delivering the best performance.
- It, like other quadratic sorting algorithms, is efficient for small data sets.
- It just necessitates a constant amount of O(1) extra memory space.
- It works well with data sets that have been sorted in a significant way.
- It does not affect the relative order of elements with the same key.
What Are the Disadvantages of the Insertion Sort?
Despite its simplicity and effectiveness over smaller data sets, Insertion sort does a few downfalls. This tutorial will now address a few major drawbacks which you should consider before you implement insertion sort in real-time
- Insertion sort is inefficient against more extensive data sets
- The insertion sort exhibits the worst-case time complexity of O(n2)
- It does not perform well than other, more advanced sorting algorithms
With this, you have come to an end of this tutorial. You will now look at what could be your next steps to master sorting algorithms.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
Next Steps
Your next stop in mastering data structures should be the selection Sort Algorithm. The selection sort algorithm divides the list into two parts, with the sorted half on the left and the unsorted half on the right, using in-place comparisons.'ll study Angular, Spring Boot, Hibernate, JSPs, and MVC in this web development course, which will help you get started as a full-stack developer.
If you have any questions or require clarification on this "Merge Sort Algorithm" tutorial, please leave them in the comments section below. Our expert team will review them and respond as soon as possible.