×

Bubble Sort vs Selection Sort

In this article, we will discuss the basic differences between these two sorting algorithms. Let us have a quick overview of what these sorting algorithms are? And what are the ideas behind them?

What is Bubble Sort? and discuss the idea using an example.

Bubble Sort – The idea behind the bubble sort is to compare two adjacent elements of an array and swap them if the predecessor is greater than the successor. On each iteration, it maintains the order between two adjacent elements means a smaller value should come before a larger value. The pseudo-code of the bubble sort 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

The first for-loop starts from 1 and ends at n -1, and the second for-loop starts from 0 and ends at n - i – 1. After each complete iteration of the external for loop, the size of the sorted array is increased by one, and the size of the unsorted array is decreased by 1.

Now let us see how it works through an example.

Consider an unsorted array of 6 elements, let’s say a[] = {24, 12, 3, 6, 11, 9}. Now, we will sort the above array using the Bubble Sort.

Bubble Sort vs Selection Sort
  • First Iteration – i = 1 j = 0 to 6 - 1 – 1 = 4
    • j = 0. Here, a[0] = 24 and a[1] = 12 and 24 > 12, we swap these two values and the array be will like as shown in the figure below:
Bubble Sort vs Selection Sort
  • j = 1. Here, a[1] = 24 and a[2] = 3 and 24 > 3, we swap these two values, and the array be will like as shown in the figure below:
Bubble Sort vs Selection Sort
  • j = 2. Here, a[2] = 24 and a[3] = 6 and 24 > 6, we swap these two values, and the array will be like as shown in the figure below:
Bubble Sort vs Selection Sort
  • j = 3. Here, a[3] = 24 and a[4] = 11 and 24 > 11, we swap these two values, and the array will be like as shown in the figure below:
Bubble Sort vs Selection Sort
  • j = 4. Here, a[4] = 24 and a[5] = 9 and 24 > 9, we swap these two values, and the array will be like as shown in the figure below:
Bubble Sort vs Selection Sort
  • Second Iteration – i = 2, j = 0 to 6 – 2 – 1 = 3
    • j = 0. Here, a[0] = 12 and a[1] = 3 and 12 > 3, we swap these two values and the array will be like as shown in the figure below:
Bubble Sort vs Selection Sort
  • j = 1. Here, a[1] = 12 and a[2] = 6 and 12 > 6, we swap these two values, and the array will be like as shown in the figure below:
Bubble Sort vs Selection Sort
  • j = 2. Here, a[2] = 12 and a[3] = 11 and 12 > 11, we swap these two values, and the array will be like as shown in the figure below:
Bubble Sort vs Selection Sort
  • j = 3. Here, a[3] = 12 and a[4] = 9 and 12 > 9, we swap these two values and the array will be like as shown in the figure below:
Bubble Sort vs Selection Sort
  • Third Iteration – i = 3, j = 0 to 2
    • j = 0. Here, a[0] = 3 and a[1] = 3 and 3 < 6, we won’t swap them and the array will be like as shown in the figure:
Bubble Sort vs Selection Sort
  • j = 1. Here, a[1] = 6 and a[2] = 11 and 6 < 11, we won’t swap them and the array will be like as shown in the figure:
Bubble Sort vs Selection Sort
  • j = 2. Here, a[2] = 11 and a[3] = 9 and 11 > 9, we swap these two values, and the array will be like as shown in the figure below:
Bubble Sort vs Selection Sort
  • Fourth Iteration – i = 4, j = 0 to 1
    • j = 0. a[0] = 3 and a[1] = 6 and 3 < 6, we won’t swap them and the array will be like as shown in the figure:
Bubble Sort vs Selection Sort
  • j = 1. a[1] = 6 and a[2] = 9 and 6 < 9, we won’t swap them and the array will be like as shown in the figure:
Bubble Sort vs Selection Sort
  • Fifth Iteration – i = 5, j = 0 to 0
    • j = 1. a[0] = 3 and a[1] = 6 and 3 < 6, we won’t swap them and the array will be like as shown in the figure:
Bubble Sort vs Selection Sort

After completing the 5th iteration, we get our sorted array.

What is Selection Sort?

Selection sort – The idea is to find the array’s minimum element and place it at its correct position. In this sorting technique, we divide the given array into two sorted and unsorted subarrays. Initially, we assume the size of the sorted array is zero, and the size of the unsorted array is the size of the array itself. On each iteration, we find the unsorted array’s minimum element, swap it with its first element, and increase the size of the sorted array by one.

The pseudo-code of the Selection sort function is given below:

Selection Sort (int a[], int n) 
{
    For i = 0 to n - 2 
    {
        Min_index = i; 
        For j = i+1 to n - 1 
        {
            If (a[j] < a[Min_index]) 
                Min_index = j;
        }
        If i != min_index
       	      swap(a[i], a[Min_index]); 
    }
} // Where Min_index is the index of the minimum element and n is the size of the array.

The first for loop starts from 0 and ends at n – 2, and the second for loop starts from i + 1 and ends at n – 1. After each complete execution of the internal for loop, the unsorted array’s minimum element is placed at its correct position in the sorted array.

Let us understand this through an example. We take an unsorted array a[] = {24, 12, 3, 6, 11, 9} of size = 6.

  • First iteration – i = 0, j = 1 to 6 – 1 = 5, min_index = i = 0
    • J = 1, a [j = 1] = 12 and a[min_index = 0] = 24 and 12 < 24. If condition is satisfied, we update the min_index = j = 1.
    • J = 2, a[ j = 2] = 3 and a[min_index = 1] = 3 and 3 < 12. If condition is satisfied, we update the min_index = j = 2.
    • J = 3, a[ j = 3] = 6 and a[min_index = 2] = 3 and 6 > 3. If condition is not statisfied.
    • J = 4, a[ j = 4] = 11 and a[min_index = 2] = 3 and 11 > 3. If condition is not statisfied.
    • J = 5, a[ j = 5] = 9 and a[min_index = 2] = 3 and 9 > 3. If condition is not statisfied.

We swap a[i = 0] = 24 and a[min_index = 2] = 3. Now, the array will look like as shown in the figure.

Bubble Sort vs Selection Sort
  • Second Iteration – i = 1, j = 2 to 5, min_index = i = 1
    • J = 2, a[ j = 2] = 24 and a[min_index = 1] = 12 and 12 < 24. If condition is not statisfied.
    • J = 3, a[ j = 3] = 6 and a[min_index = 1] = 12 and 6 < 12. If condition is satisfied, we update the min_index = j = 3.
    • J = 4, a[ j = 4] = 11 and a[min_index = 3] = 6 and 11 > 6. If condition is not statisfied.
    • J = 5, a[ j = 5] = 9 and a[min_index = 3] = 6 and 9 > 6. If condition is not statisfied.

We swap a[i = 1] = 12 and a[min_index = 3] = 6. Now, the array will look like as shown in the figure.

Bubble Sort vs Selection Sort
  • Third Iteation – i  = 2, j = 3 to 5, min_index = i = 2
    • J = 3, a[ j = 3] = 12 and a[min_index = 2] = 24 and 12 < 24. If condition is satisfied, we update the min_index = j = 3.
    • J = 4, a[j = 4] = 11 and a[min_index = 3] = 12 and 11 < 12. If condition is satisfied, we update the min_index = j = 4.
    • J = 5, a[j = 5] = 9 and a[min_index = 4] = 11 and 9 < 11. If condition is satisfied, we update the min_index = j = 5.

We swap a[i = 2] = 24 and a[min_index = 5] = 9. Now, the array will look like as shown in the figure.

Bubble Sort vs Selection Sort
  • Fourth Iteration – i = 3, j = 4 to 5, min_index = i = 3
    • J = 4, a[j = 4] = 11 and a[min_index = 3] = 12 and 11 < 12. If condition is satisfied, we update the min_index = j = 4.
    • J = 5, a[ j = 5] = 24 and a[min_index = 4] = 11 and 24 > 11. If condition is not statisfied.

We swap a[i = 3] = 12 and a[min_index = 4] = 11. Now, the array will look like as shown in the figure.

Bubble Sort vs Selection Sort
  • Fifth Iteration – i = 4, j = 5 to 5, min_index = i = 4
    • J = 5, a[ j = 5] = 24 and a[min_index = 4] = 12 and 24 > 12. If condition is not satisfied.

Here, i is equal to min_index, no swapping occurs, and after completing the fifth iteration, we get our sorted array.

Bubble Sort vs Selection Sort

Differences between Bubble Sort and Selection Sort:

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

Bubble Sort vs Selection Sort
 Bubble SortSelection Sort
Idea or ApproachThe idea is to compare two adjacent elements in the unsorted array and place them in increasing or decreasing order.The idea is to find the smallest element in the unsorted array and place it in its correct position.
Time ComplexityBest case: T(n) = O(n) Average case: T(n) = O(n^2) Worst case: T(n) = O(n^2)Best case: T(n) = O(n^2) Average case: T(n) = O(n^2) Worst case: T(n) = O(n^2)
Space ComplexityWorst case: T(n) = O(1)Worst case: T(n) = O(1)
EfficiencyLess efficient compared to Selection SortMore efficient compared to Bubble Sort
SpeedSlower than selection sort as many comparisons and swapping are required.Much faster than the bubble sort, a smaller number of comparisons and swapping are required.
StabilityStable – The order of the elements remains preserved.Unstable – The order of the elements doesn’t remain preserved.
MethodThe comparison method is used to sort an array.The selection method is used to sort an array.

Related Topics

Top view of binary tree

We know that a binary tree is a kind of tree that helps us organize our tree and that it is a kind of non-linear info structure that at least...

4 minutes read.

Finding the Sum of All Paths in a Binary Tree

Implementation // Writing the C++ program to implement the below approach.  #include <bits/stdc++.h> using namespace std; // creating the new tree node structure. struct Tree__nod { int val; Tree__nod *Lft, *Rt; }; // creating a new function that will...

8 minutes read.

Binary Search

Binary Search: When there is a large data structure, the linear search takes a lot of time to search the element. The binary search was developed to overcome the lack...

7 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.

Tree terminology in Data structures

Data structures The storage used to organize and store data is known as a data structure, and it is a method where data can be arranged on a computer to be...

6 minutes read.

Given a Binary Tree, find its Minimum Depth

Implementation // Creating a C++ program or implementation to search and explore the minimum depth of a given binary tree.  #include<bits/stdc++.h> using namespace std; // Creating a new binary tree node struct __nod { int record; struct __nod*...

5 minutes read.

Bin Packing Problem (How to minimize the number of used Bins)

You have been given an array. The values of the array represent the size of n different items. You have been also given some bins. You have to store the...

3 minutes read.

Optimal binary search tree in DSA

Implementation // A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.   #include <bits/stdc++.h> using namespace std; // we have to create a basic utility...

8 minutes read.

Length of longest palindrome in a linked list using O(1) extra space

Length of longest palindrome in a linked list using O(1) extra space In this problem, we need to find the length of the longest palindrome list that is present in given...

2 minutes read.

Radix Sort

Radix Sort: The radix sort is a non-comparative integer sorting algorithm that sorts the elements by grouping the individual digits of the same location. It shares the same significant position...

4 minutes read.

Horizontal and Vertical Scaling

Being a software engineer, you would have designed a website or application and deployed it on any server. Imagine that the developed application starts getting popular, and many users engage...

6 minutes read.

Partitioning a linked list around a given value

Partitioning a linked list around a given value In this problem, we are given a linked list and a value k. We need to partition the given linked list so that...

3 minutes read.

Binary Search Tree

Binary Search Tree: A binary search tree is a type of tree in which every node is organized in the sorted order. It is also called an ordered binary tree. Properties...

4 minutes read.

What Should We Learn First? Trees or Graphs in Data Structures

A data structure is a database used to store and manage data and optimize and manage computing resources. A data structure is a form used intelligently and quickly to store,...

6 minutes read.

Types of Data Structures

Almost every programme or software system that has been built makes use of data structures. Furthermore, data structures are basics of computer science and software engineering. When it comes to...

7 minutes read.

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...

4 minutes read.

Tim Sort

Tim Sort is a mixture stable arranging calculation that exploits normal examples in information, and uses a mix of an improved Merge sort and Binary Insertion sort alongside an interior...

6 minutes read.

Hash Table vs STL Map

Hash table and STL map are extremely valuable information structures in software engineering. Here we will consider the examination between their properties to be well as execution.  To start with, we will...

7 minutes read.

Find all possible words from board

We have been given a dictionary of words and a board of characters from which we can form strings. Now, we have to check if the string is present in...

5 minutes read.

Asynchronous advantage actor-critic (A3C) Algorithm

The Asynchronous advantage actor-critic (A3C) Algorithm is one of the latest algorithms developed by the Artificial Intelligence division, Deep Mind at Google. It is used for the Deep Reinforcement Learning...

3 minutes read.