×

Insertion Sort vs Bubble Sort

In this article, we will see the major differences between Insertion Sort and Bubble Sort. Before that, let’s have a quick overview of what these sorting algorithms are and what’s the idea behind them.

What is Insertion Sort?

Insertion Sort – In Insertion sort, we select all elements of the unsorted array and place them at their correct position in the sorted array. After each iteration, the sorted array’s size increases and the unsorted array's size decreases. The pseudo-code of the insertion sort function is given below:

Insertion sort (a[], n)
{
	For j 1 to n-1
	{	
		Key = a[j]
		i = j - 1
		While i > -1 and a[i] > key
		{
			a[i + 1] = a[i]
			i = i – 1
		}
	a[i + 1] = Key
	}
} // Where n is the size of the array

The for-loop runs from 1 to n-1 and the while-loop runs from i = j-1 to 0. After complete execution of the internal while-loop, the element at the jth index is placed at its correct position in the sorted array.

Let’s understand the above idea through an example-

Consider an unsorted array a[] = {25, 10, 12, 18, 6}. Let’s apply insertion sort on it:

Insertion Sort Vs Bubble Sort
  • First Iteration – j = 1, i = 0 to 0, and Key = a[j] = a[1] = 10
    • Here, i is greater than -1 and a[i = 0] = 25 > key = 10. The condition of while loop is satisfied, we place 25 at index 1, and decrement i by 1.
Insertion Sort Vs Bubble Sort
  • Now, i is equal to -1, hence the condition is not satisfied. We execute the last statement. a[i + 1] = a[-1 + 1] = a[0] = Key = 10. Now, the array will be like as shown in figure.
Insertion Sort Vs Bubble Sort
  • Second Iteration – j = 2, i = 1 to 0, and Key = a[2] = 12.
    • Here, i = 1, a[i] = 25 > Key, the while-condition is satisfied. We execute a[1+1] = a[1] = 25 and i = 1-1 = 0.
Insertion Sort Vs Bubble Sort
  • Now, i = 0 and a[0] = 10 < key = 12. The while condition is not satisfied. We execute the last statement, a[i+1] = a[0+1] = 12.
Insertion Sort Vs Bubble Sort
  • Third Iteration – j = 3, i = 2 to 0, and Key = a[3] = 18
    • Here, i = 2, a[i] = 25 > Key = 18. The while condition is satisfied. We execute a[2+1] = a[2] = 25 and i = 2 - 1 = 1
Insertion Sort Vs Bubble Sort
  • Now, i = 1, a[i] = 12 < Key = 18. The while condition is not satisfied. We execute the last statement, a[i + 1] = a[1 + 1] = 18.
Insertion Sort Vs Bubble Sort
  • Fourth Iteration – j = 4, i = 3 to 0, and Key = a[4] = 6.
    • Here, i = 3, a[i] = 25 > Key = 6. The while condition is satisfied. We execute a[3 + 1] = a[3] = 25 and i = 3 - 1 = 2.
Insertion Sort Vs Bubble Sort
  • Now, i = 2, a[i] = 18 > Key = 6. The while condition is satisfied. We execute a[2 + 1] = a[2] = 18 and i = 2 - 1 = 1.
Insertion Sort Vs Bubble Sort
  • Now, i = 1, a[i] = 12 > Key = 6. The while condition is satisfied. We execute a[1 + 1] = a[1] = 12 and i = 1 - 1 = 0.
Insertion Sort Vs Bubble Sort
  • Now, i = 0, a[i] = 10 > Key = 6. The while condition is satisfied. We execute a[0 + 1] = a[0] = 10 and i = 0 - 1 = -1.
Insertion Sort Vs Bubble Sort
  • Now, i is equal to -1, hence the condition is not satisfied. We execute the last statement. a[i + 1] = a[-1 + 1] = a[0] = Key = 6. Now, the array will be like as shown in figure.
Insertion Sort Vs Bubble Sort

After complete execution of the 4th iteration, we get our sorted array.

What is Bubble Sort?

Bubble Sort – In bubble sort, it continuously compares two adjacent elements and swaps them if they are not in the right order on each iteration. On each pass, it places the largest value at the end of the unsorted array. The pseudo-code of the bubble sort function is given below:

Bubble Sort (int a[], int n)
{
	For i =1 to n-1
	{
		for j = 0 to n - i – 1
		{
			If a[j] > a[j + 1]
				swap (a[j], a[j + 1])
		}
	}
} // Where n is the size of the array

We can clearly see in the above pseudo-code that the external for loop starts from 1 and ends at n - 1, and the internal for-loop starts from 0 and ends at n - i - 1.

Let’s understand the above idea through an example:

Consider an unsorted array a[5] = {25, 10, 12, 18, 6}. Let’s sort this array using bubble sort.

Insertion Sort Vs Bubble Sort
  • First Iteration – i = 1, j 0 to n – 1 – 1 = 3.
    • j = 0. Here, a[0] = 25 and a[1] = 10 and 25 > 10, If-condition is satisfied, we swap these values.
Insertion Sort Vs Bubble Sort
  • j = 1. Here, a[1] = 25 and a[2] = 12 and 25 > 12, If-condition is satisfied, we swap these values.
Insertion Sort Vs Bubble Sort
  • j = 2. Here, a[2] = 25 and a[3] = 18 and 25 > 18, If-condition is satisfied, we swap these values:
Insertion Sort Vs Bubble Sort
  • j = 3. Here, a[3] = 25 and a[4] = 6 and 25 > 16, If-condition is satisfied, we swap these values:
Insertion Sort Vs Bubble Sort
  • Second Iteration – i = 2, j = 0 to 2
    • j = 0. Here, a[0] = 10 and a[1] = 12 and 10 < 12, If-condition is not satisfied, no swapping occurs:
Insertion Sort Vs Bubble Sort
  • j = 1. Here, a[1] = 12 and a[2] = 18 and 12 < 18, If-condition is not satisfied, no swapping occurs:
Insertion Sort Vs Bubble Sort
  • j = 2. Here, a[2] = 18 and a[3] = 6 and 18 > 6, If-condition is satisfied, we swap these values:
Insertion Sort Vs Bubble Sort
  • Third Iteration – i = 3, j 0 to 1
    • j = 0. Here, a[0] = 10 and a[1] = 12 and 10 < 12, If-condition is not satisfied, no swapping occurs:
Insertion Sort Vs Bubble Sort
  • j = 1. Here, a[1] = 12 and a[2] = 6 and 12 > 6, If-condition is satisfied, we swap these values:
Insertion Sort Vs Bubble Sort
  • Fourth Iteration – i = 4, j 0 to 0
    • j = 0. Here, a[0] = 10 and a[1] = 6 and 10 > 6, If-condition is satisfied, we swap these values.
Insertion Sort Vs Bubble Sort

After complete execution of the external for loop, we get a sorted array.

Differences between Insertion Sort and Bubble Sort:

The main differences between these two sorting techniques are listed below in the table:

Insertion Sort Vs Bubble Sort

Insertion Sort

Bubble Sort

Idea/

Approach

The idea is to insert an element of the unsorted array at its correct position in the sorted array.

The idea is to compare every place and place them in the right order.

Time Complexity

Best

Average

Worst

Best

Average

Worst

T(n) = O(n)

T(n) = O(n^2)

T(n) = O(n^2)

T(n) = O(n)

T(n) = O(n^2)

T(n) = O(n^2)

Space Complexity

Worst Case: T(n) = O(1)

Worst Case: T(n) = O(1)

Efficiency

More efficient than the Bubble Sort.

Less Efficient than the Insertion Sort

Speed

Faster than the Bubble Sort as less comparison and swapping occur.

Slower than the Insertion sort as more comparisons and swapping occur.

Stability

Stable – Element’s orders remain preserved

Stable – Element’s Orders remain preserved

Method

The insertion method is used to sort an array.

The comparison method is used to sort an array.


Related Topics

Bitwise Operators and their Important Tricks

In most of the programs you write today, you deal with data types comprising bytes, such as integer, float, double, etc. Dealing with bytes? It is a quite normal task,...

5 minutes read.

Difference Between Linear and Non Linear Data Structures

Data Structure A data structure is a data object together with the relationships between the instances and the individual elements that compose an instance. These relationships are defined by the operations...

5 minutes read.

Given a Binary Tree Check the Zig-Zag Traversal

Implementation // The C++ implementation of the zig-zag traversal method in the O(n) time.  #include <iostream> #include <stack> using namespace std; // creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a...

4 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Box Stacking Problem

Stacking of boxes depending on their base You have been given n different boxes. These boxes will have different heights, widths, and depths. You have to stack all these boxes in...

4 minutes read.

Array Data Structure

Data Structure Array: The array is a non-primitive and linear data structure that is a group of similar data items. That is, it can store only one type of data....

6 minutes read.

Given a Binary Tree Return All Root-to-Leaf Paths

Implementation #include <bits/stdc++.h> using namespace std; // A binary tree node generally consists of data, a pointer to the left and right child, and a pointer to the right child.  class __nod { public: int record; __nod* Lft; __nod*...

9 minutes read.

Applications of Different Linked Lists in Data Structure

What is a Linked list? A linked list is a data structure that consists of a sequence of elements, where each containing a reference or ("link") to the next element in...

5 minutes read.

Threaded Binary Trees

Introduction Threaded Binary Trees (TBTs) are an enhancement of normal binary trees intended for in-order traversal only. This means that this data structure is developed with the objective of making the...

12 minutes read.

Sum of Nodes in a Binary Tree

In this article, we will see the sample problems that will help us understand the concept and summation of all the nodes in the binary tree. Implementation /* creating a program that...

4 minutes read.

Strictly binary tree in Data Structures?

What is a strictly Binary Tree in Data Structures? There are various kinds of binary trees that we know exist in data structures, and they all have their purposes. In this...

4 minutes read.

Semi-Structured data

In this article, we will discuss the semi-structured data. Data can be defined as the distinct piece of information that is gathered and translated for some purpose. It can be...

5 minutes read.

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language. Implementation #include <iostream> using namespace std; class B_TreeNod {   int *kys;   int m;   BTreeNod **C;   int j;   bool leaf;  ...

5 minutes read.

Collision Resolution Techniques

Collision Resolution Techniques Collision in hashing In this, the hash function is used to compute the index of the array.The hash value is used to store the key in the hash table,...

2 minutes read.

Recursion in Fibonacci

Fibonacci heap is considered to be a particular execution of the heap data structure that ultimately helps in making use of not just any number but the Fibonacci numbers. It...

3 minutes read.

About Data Structures

What exactly are data structures? A data structure is a type of storage that is used to organise and store data. It is a method of organising data on a computer...

5 minutes read.

Heap Sort in Data Structure

Heap Sort A heap is a tree-based data structure that has specific properties. Heap is always a complete binary tree (CBT). That is, all the nodes of the tree are completely filled.If...

6 minutes read.

What is a Sparse Matrix in Data Structure?

Definition A matrix in which a few non-zero elements are present is called a Sparse matrix. In a Sparse matrix, almost all the matrices are filled with zero (0). A matrix...

5 minutes read.

Check if a Singly Linked List is Palindrome

Check if a Singly Linked List is Palindrome In this section, we have given a singly linked list, and we need to check whether the given list is a palindrome. Example:           1...

3 minutes read.

Stack vs Queue: Data Structure

 Difference Between Stack and Queue What is Stack? The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first....

3 minutes read.