Understand Bubble Sort Problem

Problem Name: Bubble Sort
Problem Description:

Problem: Bubble Sort

Difficulty: Easy
Topics: Sorting Algorithms, Iterative Methods
Companies: Commonly asked in technical interviews.


Problem Description

Bubble sort is a sorting algorithm that repeatedly compares adjacent elements in an array and swaps them if they are in the wrong order. This process continues until the array is completely sorted.


How Bubble Sort Works

1. Outer Loop:

  • The outer loop iterates from the beginning to the end of the array.
  • With each iteration, the algorithm focuses on sorting the unsorted portion of the array.

2. Inner Loop:

  • The inner loop compares each pair of adjacent elements in the array:
    • If the preceding element is greater than the subsequent element, swap them.

3. Largest Element Bubbles Up:

  • After one complete iteration of the inner loop, the largest element in the unsorted portion of the array moves to its correct position at the end.
  • This is why it’s called “Bubble Sort,” as larger elements ‘bubble up’ to the top.

4. Repeat:

  • Continue the process for the remaining unsorted portion of the array until the entire array is sorted.

Key Idea

Bubble sort works by iteratively ensuring that, after each pass, the largest unsorted element moves to its correct position. After every iteration, the sorted portion of the array grows from right to left.


Time Complexity

  • Best Case (Sorted Array): 𝑂(𝑛) with optimization (check if no swaps are needed in an iteration).
  • Worst Case (Reversed Array): 𝑂(𝑛²).
  • Bubble sort is inefficient for large datasets compared to more advanced algorithms like quicksort or mergesort.

Example Execution

Unsorted Array:

[5, 1, 4, 2, 8]

Step-by-Step Process:

  1. First Pass: Compare and swap adjacent elements.
    Result: [1, 4, 2, 5, 8] (largest element 8 bubbles up to the end).
  2. Second Pass: Compare and swap adjacent elements in the remaining unsorted portion.
    Result: [1, 2, 4, 5, 8] (5 bubbles up to its correct position).
  3. Third Pass: No swaps are needed, indicating the array is sorted.

Final Sorted Array:

[1, 2, 4, 5, 8]


Why Use Bubble Sort?

  • Advantages:

    • Simple and easy to understand.
    • Can be optimized to stop early if no swaps are needed in an iteration.
  • Disadvantages:

    • Inefficient for large datasets due to its quadratic time complexity.
    • Not suitable for performance-critical applications.

Practical Tip

Bubble sort is primarily used for educational purposes to demonstrate the concept of sorting algorithms. For larger or more complex datasets, use more efficient algorithms like quicksort or heapsort.

Category:
  • Array
Programming Language:
  • Java
Reference Link:

https://www.geeksforgeeks.org/problems/bubble-sort/1?itm_source=geeksforgeeks&itm_medium=article&itm_campaign=bottom_sticky_on_article

Online IDE

Scroll down for output
Java
Output:

Loading component...

Loading component...

Tracking code (View only. In case you want to track the code, click this button):
Main Function:

INPUT: int [] arr = {4,3,2,1};

OUTPUT: arr = {1,2,3,4}

public static void main(String[] args){

int[] arr = { 64, 25, 12, 22, 11 };

System.out.print("Original array: ");

printArray(arr);

bubbleSort(arr, arr.length);

System.out.print("Sorted array: ");

printArray(arr);

} // Function End

Helper Function:

static void bubbleSort(int arr[] , int n){

int i, j;

boolean swapped = false;

for(i =0;i<n-1; i++){

for(j =0;j<n-i-1; j++){

swapped = false;

if(arr[j] > arr[j+1]){

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

swapped = true;

}//If End

}//Loop End

if(!swapped){

break;

} //If End

}//Loop End

}//function end

Utility Functions and Global variables:

static void printArray(int[] arr){

for (int val : arr) {

System.out.print(val + " ");

}//Loop End

System.out.println();

} // Function End