Saturday, November 16, 2024
Google search engine
HomeData Modelling & AIComparison among Bubble Sort, Selection Sort and Insertion Sort

Comparison among Bubble Sort, Selection Sort and Insertion Sort

Bubble Sort, Selection Sort, and Insertion Sort are simple sorting algorithms that are commonly used to sort small datasets or as building blocks for more complex sorting algorithms. Here’s a comparison of the three algorithms:

Bubble Sort:

  1. Time complexity: O(n^2) in the worst and average cases, O(n) in the best case (when the input array is already sorted)
    Space complexity: O(1)
  2. Basic idea: Iterate through the array repeatedly, comparing adjacent pairs of elements and swapping them if they are in the wrong order. Repeat until the array is fully sorted.

Selection Sort:

  1. Time complexity: O(n^2) in all cases (worst, average, and best)
    Space complexity: O(1)
  2. Basic idea: Find the minimum element in the unsorted portion of the array and swap it with the first unsorted element. Repeat until the array is fully sorted.

Insertion Sort:

  1. Time complexity: O(n^2) in the worst and average cases, O(n) in the best case (when the input array is already sorted)
    Space complexity: O(1)
  2. Basic idea: Build up a sorted subarray from left to right by inserting each new element into its correct position in the subarray. Repeat until the array is fully sorted.

Comparison:

  1. Bubble Sort and Selection Sort have the same worst-case time complexity of O(n^2), while Insertion Sort is slightly better with an average-case time complexity of O(n^2).
  2. Insertion Sort has the best-case time complexity of O(n) when the input array is already sorted, which is not possible for Bubble Sort and Selection Sort.
  3. Selection Sort and Insertion Sort both have the same space complexity of O(1), while Bubble Sort also has a space complexity of O(1).
  4. Bubble Sort and Insertion Sort are stable sorting algorithms, meaning that they preserve the relative order of equal elements in the sorted array, while Selection Sort is not stable.
  5. In terms of performance, Insertion Sort tends to perform better than Bubble Sort and Selection Sort for small datasets, while Bubble Sort and Selection Sort may perform better than Insertion Sort for larger datasets or datasets that are partially sorted.
    Overall, each algorithm has its own advantages and disadvantages, and the choice of which algorithm to use depends on the specific requirements of the problem at hand.

Sure! Here are some advantages and disadvantages of each algorithm based on the same code and dataset (Python implementation of sorting 10000 random integers):

Bubble Sort:

Advantages: Simple implementation, works well for small datasets, requires only constant space, stable sorting algorithm
Disadvantages: Inefficient for large datasets, worst-case time complexity of O(n^2), not optimal for partially sorted datasets

Selection Sort:

Advantages: Simple implementation, works well for small datasets, requires only constant space, in-place sorting algorithm
Disadvantages: Inefficient for large datasets, worst-case time complexity of O(n^2), not optimal for partially sorted datasets, not a stable sorting algorithm

Insertion Sort:

Advantages: Simple implementation, works well for small datasets, requires only constant space, efficient for partially sorted datasets, stable sorting algorithm
Disadvantages: Inefficient for large datasets, worst-case time complexity of O(n^2)

Code (Python implementation):

Java




import java.util.Random;
 
public class SortingAlgorithms {
 
    public static void bubbleSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
 
    public static void selectionSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }
 
    public static void insertionSort(int[] arr) {
        int n = arr.length;
        for (int i = 1; i < n; i++) {
            int key = arr[i];
            int j = i - 1;
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = key;
        }
    }
 
    public static void main(String[] args) {
        int[] arr = new int[10000];
        Random rand = new Random();
        for (int i = 0; i < 10000; i++) {
            arr[i] = rand.nextInt(10000) + 1;
        }
 
        long startTime = System.nanoTime();
        bubbleSort(arr.clone());
        long bubbleSortTime = System.nanoTime() - startTime;
 
        startTime = System.nanoTime();
        selectionSort(arr.clone());
        long selectionSortTime = System.nanoTime() - startTime;
 
        startTime = System.nanoTime();
        insertionSort(arr.clone());
        long insertionSortTime = System.nanoTime() - startTime;
 
        System.out.println("Bubble Sort time: " + bubbleSortTime);
        System.out.println("Selection Sort time: " + selectionSortTime);
        System.out.println("Insertion Sort time: " + insertionSortTime);
    }
}


Python3




import random
 
def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
 
def selection_sort(arr):
    n = len(arr)
    for i in range(n):
        min_index = i
        for j in range(i + 1, n):
            if arr[j] < arr[min_index]:
                min_index = j
        arr[i], arr[min_index] = arr[min_index], arr[i]
 
def insertion_sort(arr):
    n = len(arr)
    for i in range(1, n):
        key = arr[i]
        j = i - 1
        while j >= 0 and arr[j] > key:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key
 
# Generate a list of 10000 random integers
arr = [random.randint(1, 10000) for i in range(10000)]
 
# Sort the list using each algorithm and time it
import time
 
start_time = time.time()
bubble_sort(arr.copy())
bubble_sort_time = time.time() - start_time
 
start_time = time.time()
selection_sort(arr.copy())
selection_sort_time = time.time() - start_time
 
start_time = time.time()
insertion_sort(arr.copy())
insertion_sort_time = time.time() - start_time
 
print("Bubble Sort time:", bubble_sort_time)
print("Selection Sort time:", selection_sort_time)
print("Insertion Sort time:", insertion_sort_time)


C#




using System;
using System.Diagnostics;
using System.Linq;
 
public class SortingAlgorithms {
    public static void BubbleSort(int[] arr) {
        int n = arr.Length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
 
    public static void SelectionSort(int[] arr) {
        int n = arr.Length;
        for (int i = 0; i < n; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }
 
    public static void InsertionSort(int[] arr) {
        int n = arr.Length;
        for (int i = 1; i < n; i++) {
            int key = arr[i];
            int j = i - 1;
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = key;
        }
    }
 
    public static void Main(string[] args) {
        int[] arr = new int[10000];
        Random rand = new Random();
        for (int i = 0; i < 10000; i++) {
            arr[i] = rand.Next(10000) + 1;
        }
 
        Stopwatch sw = new Stopwatch();
 
        sw.Start();
        BubbleSort((int[])arr.Clone());
        sw.Stop();
        long bubbleSortTime = sw.ElapsedTicks;
 
        sw.Restart();
        SelectionSort((int[])arr.Clone());
        sw.Stop();
        long selectionSortTime = sw.ElapsedTicks;
 
        sw.Restart();
        InsertionSort((int[])arr.Clone());
        sw.Stop();
        long insertionSortTime = sw.ElapsedTicks;
 
        Console.WriteLine("Bubble Sort time: " + bubbleSortTime);
        Console.WriteLine("Selection Sort time: " + selectionSortTime);
        Console.WriteLine("Insertion Sort time: " + insertionSortTime);
    }
}


Javascript




// Function to generate a random integer between min (inclusive) and max (inclusive)
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
 
// Function to perform Bubble Sort
function bubbleSort(arr) {
  const n = arr.length;
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        // Swap arr[j] and arr[j + 1]
        const temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
    }
  }
}
 
// Function to perform Selection Sort
function selectionSort(arr) {
  const n = arr.length;
  for (let i = 0; i < n; i++) {
    let minIndex = i;
    for (let j = i + 1; j < n; j++) {
      if (arr[j] < arr[minIndex]) {
        minIndex = j;
      }
    }
    // Swap arr[i] and arr[minIndex]
    const temp = arr[i];
    arr[i] = arr[minIndex];
    arr[minIndex] = temp;
  }
}
 
// Function to perform Insertion Sort
function insertionSort(arr) {
  const n = arr.length;
  for (let i = 1; i < n; i++) {
    const key = arr[i];
    let j = i - 1;
    while (j >= 0 && arr[j] > key) {
      arr[j + 1] = arr[j];
      j--;
    }
    arr[j + 1] = key;
  }
}
 
// Main function
function main() {
  const arr = new Array(10000);
 
  // Populate the array with random values
  for (let i = 0; i < 10000; i++) {
    arr[i] = getRandomInt(1, 10000); // Adjust the range as needed
  }
 
  const startTimeBubble = performance.now();
  bubbleSort(arr);
  const bubbleSortTime = performance.now() - startTimeBubble;
 
  const startTimeSelection = performance.now();
  selectionSort([...arr]);
  const selectionSortTime = performance.now() - startTimeSelection;
 
  const startTimeInsertion = performance.now();
  insertionSort([...arr]);
  const insertionSortTime = performance.now() - startTimeInsertion;
 
  console.log("Bubble Sort time: " + bubbleSortTime.toFixed(2) + " ms");
  console.log("Selection Sort time: " + selectionSortTime.toFixed(2) + " ms");
  console.log("Insertion Sort time: " + insertionSortTime.toFixed(2) + " ms");
}
 
// Call the main function
main();


Output:

Bubble Sort time: 16.710935354232788
Selection Sort time: 7.3090105056762695
Insertion Sort time: 0.003000974655151367
 

In this example, we use the same code to sort a list of 10000 random integers using Bubble Sort, Selection Sort, and Insertion Sort. We then time each algorithm’s execution using the time module.

As shown in the output, Insertion Sort is much faster than Bubble Sort and Selection Sort for this dataset. However, it’s important to note that the performance of each algorithm can vary depending on the specific characteristics of the dataset.

1. Bubble Sort

Bubble sort repeatedly compares and swaps(if needed) adjacent elements in every pass. In i-th pass of Bubble Sort (ascending order), last (i-1) elements are already sorted, and i-th largest element is placed at (N-i)-th position, i.e. i-th last position. 
Algorithm: 

BubbleSort (Arr, N) // Arr is an array of size N.
{
For ( I:= 1 to (N-1) ) // N elements => (N-1) pass
{
// Swap adjacent elements of Arr[1:(N-I)]such that
// largest among { Arr[1], Arr[2], ..., Arr[N-I] } reaches to Arr[N-I]
For ( J:= 1 to (N-I) ) // Execute the pass
{
If ( Arr [J] > Arr[J+1] )
Swap( Arr[j], Arr[J+1] );
}
}
}

Optimization of Algorithm: Check if there happened any swapping operation in the inner loop (pass execution loop) or not. If there is no swapping in any pass, it means the array is now fully sorted, hence no need to continue, stop the sorting operation. So we can optimize the number of passes when the array gets sorted before the completion of all passes. And it can also detect if the given / input array is sorted or not, in the first pass. 

BubbleSort (Arr, N) // Arr is an array of size N.
{
For ( I:= 1 to (N-1) ) // N elements => (N-1) pass
{
// Swap adjacent elements of Arr[1:(N-I)]such that
// largest among { Arr[1], Arr[2], ..., Arr[N-I] } reaches to Arr[N-I]
noSwap = true; // Check occurrence of swapping in inner loop
For ( J:= 1 to (N-I) ) // Execute the pass
{
If ( Arr [J] > Arr[J+1] )
{
Swap( Arr[j], Arr[J+1] );
noSwap = false;
}
}
If (noSwap) // exit the loop
break;
}
}

Time Complexity

  • Best Case Sorted array as input. Or almost all elements are in proper place. [ O(N) ]. O(1) swaps.
  • Worst Case: Reversely sorted / Very few elements are in proper place. [ O(N2) ] . O(N2) swaps.
  • Average Case: [ O(N2) ] . O(N2) swaps.

Space Complexity: A temporary variable is used in swapping [ auxiliary, O(1) ]. Hence it is In-Place sort. 
Advantage:  

  1. It is the simplest sorting approach.
  2. Best case complexity is of O(N) [for optimized approach] while the array is sorted.
  3. Using optimized approach, it can detect already sorted array in first pass with time complexity of O(N).
  4. Stable sort: does not change the relative order of elements with equal keys.
  5. In-Place sort.

Disadvantage:  

  1. Bubble sort is comparatively slower algorithm.
  2. Poor efficiency for large elements of array.

2. Selection Sort

Selection sort selects i-th smallest element and places at i-th position. This algorithm divides the array into two parts: sorted (left) and unsorted (right) subarray. It selects the smallest element from unsorted subarray and places in the first position of that subarray (ascending order). It repeatedly selects the next smallest element. 
Algorithm: 

SelectionSort (Arr, N) // Arr is an array of size N.
{
For ( I:= 1 to (N-1) ) // N elements => (N-1) pass
{
// I=N is ignored, Arr[N] is already at proper place.
// Arr[1:(I-1)] is sorted subarray, Arr[I:N] is unsorted subarray
// smallest among { Arr[I], Arr[I+1], Arr[I+2], ..., Arr[N] } is at place min_index

min_index = I;
For ( J:= I+1 to N ) // Search Unsorted Subarray (Right lalf)
{
If ( Arr [J] < Arr[min_index] )
min_index = J; // Current minimum
}
// Swap I-th smallest element with current I-th place element
If (min_Index != I)
Swap ( Arr[I], Arr[min_index] );

}
}

Time Complexity

  • Best Case [ O(N2) ]. And O(1) swaps.
  • Worst Case: Reversely sorted, and when the inner loop makes a maximum comparison. [ O(N2) ] . Also, O(N) swaps.
  • Average Case: [ O(N2) ] . Also O(N) swaps.

Space Complexity: [ auxiliary, O(1) ]. In-Place sort.(When elements are shifted instead of being swapped (i.e. temp=a[min], then shifting elements from ar[i] to ar[min-1] one place up and then putting a[i]=temp).  If swapping is opted for, the algorithm is not In-place.) 
Advantage:  

  1. It can also be used on list structures that make add and remove efficient, such as a linked list. Just remove the smallest element of unsorted part and end at the end of sorted part.
  2. The number of swaps reduced. O(N) swaps in all cases.
  3. In-Place sort.

Disadvantage

  1. Time complexity in all cases is O(N2), no best case scenario.
  2. It requires n-squared number of steps for sorting n elements.
  3. It is not scalable.

3. Insertion Sort

Insertion Sort is a simple comparison based sorting algorithm. It inserts every array element into its proper position. In i-th iteration, previous (i-1) elements (i.e. subarray Arr[1:(i-1)]) are already sorted, and the i-th element (Arr[i]) is inserted into its proper place in the previously sorted subarray. 
Find more details in this GFG Link
Algorithm: 

InsertionSort (Arr, N) // Arr is an array of size N.
{
For ( I:= 2 to N ) // N elements => (N-1) pass
{
// Pass 1 is trivially sorted, hence not considered
// Subarray { Arr[1], Arr[2], ..., Arr[I-I] } is already sorted

insert_at = I; // Find suitable position insert_at, for Arr[I]
// Move subarray Arr [ insert_at: I-1 ] to one position right

item = Arr[I]; J=I-1;
While ( J ? 1 && item < Arr[J] )
{
Arr[J+1] = Arr[J]; // Move to right
// insert_at = J;
J--;
}
insert_at = J+1; // Insert at proper position
Arr[insert_at] = item; // Arr[J+1] = item;
}
}
}

Time Complexity:  

  • Best Case Sorted array as input, [ O(N) ]. And O(1) swaps.
  • Worst Case: Reversely sorted, and when inner loop makes maximum comparison, [ O(N2) ] . And O(N2) swaps.
  • Average Case: [ O(N2) ] . And O(N2) swaps.

Space Complexity: [ auxiliary, O(1) ]. In-Place sort. 
Advantage:  

  1. It can be easily computed.
  2. Best case complexity is of O(N) while the array is already sorted.
  3. Number of swaps reduced than bubble sort.
  4. For smaller values of N, insertion sort performs efficiently like other quadratic sorting algorithms.
  5. Stable sort.
  6. Adaptive: total number of steps is reduced for partially sorted array.
  7. In-Place sort.

Disadvantage:  

  1. It is generally used when the value of N is small. For larger values of N, it is inefficient
  2. Similar as selection sort it requires n-squared number of steps for sorting n elements.
     

Time and Space Complexity:  

Sorting Algorithm Time Complexity Space Complexity
  Best Case Average Case Worst Case Worst Case
Bubble Sort O(N) O(N2) O(N2) O(1)
Selection Sort O(N2) O(N2) O(N2) O(1)
Insertion Sort O(N) O(N2) O(N2) O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments