Bubble Sort in Design and Analysis of Algorithms (DAA)
Learning Objectives
After completing this tutorial, students will be able to:
- Understand the concept of Bubble Sort.
- Explain the working principle of Bubble Sort.
- Write the Bubble Sort algorithm and pseudocode.
- Perform a complete dry run of Bubble Sort.
- Implement Bubble Sort in C, C++, Java and Python.
- Analyze Best, Average and Worst Case complexities.
- Compare Bubble Sort with Selection Sort and Insertion Sort.
- Prepare for AKTU examinations, viva and placement interviews.
Introduction
Sorting is one of the most frequently used operations in Computer Science. Applications such as arranging student records, sorting employee data, ranking examination results and organizing products by price require efficient sorting techniques. Bubble Sort is one of the earliest sorting algorithms taught to students because of its simplicity. It repeatedly compares two adjacent elements and exchanges them whenever they are in the wrong order. After every pass, the largest element moves to its correct position at the end of the array. The same process is repeated until no more swaps are required.
Definition of Bubble Sort
Bubble Sort is a stable, comparison-based and in-place sorting algorithm that repeatedly compares adjacent elements and swaps them if they are not in the correct order. The process continues until the complete array becomes sorted in ascending or descending order.
Why is it Called Bubble Sort?
The algorithm is called Bubble Sort because after every pass, the largest unsorted element gradually moves towards the end of the array just like an air bubble rises to the surface of water.
Working Principle of Bubble Sort
Bubble Sort follows the following steps:
- Start from the first element of the array.
- Compare two adjacent elements.
- If the left element is greater than the right element, swap them.
- Move to the next pair of adjacent elements.
- Continue until the end of the array.
- Repeat the same process for the remaining unsorted elements.
- Stop when no swapping is required.
Characteristics of Bubble Sort
| Property | Description |
|---|---|
| Sorting Technique | Comparison Based |
| Sorting Method | In-place Sorting |
| Stable | Yes |
| Adaptive | Yes (Optimized Version) |
| Extra Memory | O(1) |
| Swapping | Adjacent Elements |
| Suitable For | Small Datasets |
Need of Bubble Sort
Although faster sorting algorithms such as Merge Sort and Quick Sort are available, Bubble Sort is still important because of its simplicity and educational value. Bubble Sort helps beginners understand:
- Comparison-based sorting.
- Swapping of adjacent elements.
- Iteration using loops.
- Time complexity analysis.
- Optimization using a swap flag.
- Foundation for learning advanced sorting algorithms.
Bubble Sort Algorithm
BubbleSort(A, n)
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - i - 1; j++)
{
if(A[j] > A[j + 1])
{
swap(A[j], A[j + 1]);
}
}
}
Pseudocode
START
FOR i = 0 TO n-2
FOR j = 0 TO n-i-2
IF A[j] > A[j+1]
SWAP(A[j], A[j+1])
END FOR
STOP
Flowchart of Bubble Sort
The following flowchart illustrates the complete execution process of Bubble Sort.
Basic Example
Consider the following unsorted array.
64 34 25 12 22 11 90
During the first pass, Bubble Sort repeatedly compares adjacent elements and swaps them whenever they are in the wrong order. After the first pass, the largest element (90) reaches its correct position at the end of the array. The remaining passes continue in the same manner until the complete array becomes sorted.
Dry Run of Bubble Sort
Consider the following unsorted array:
64 34 25 12 22 11 90
Bubble Sort repeatedly compares two adjacent elements and swaps them whenever they are in the wrong order. After every pass, the largest unsorted element reaches its correct position.
Pass 1
| Comparison | Action | Array |
|---|---|---|
| 64 & 34 | Swap | 34 64 25 12 22 11 90 |
| 64 & 25 | Swap | 34 25 64 12 22 11 90 |
| 64 & 12 | Swap | 34 25 12 64 22 11 90 |
| 64 & 22 | Swap | 34 25 12 22 64 11 90 |
| 64 & 11 | Swap | 34 25 12 22 11 64 90 |
| 64 & 90 | No Swap | 34 25 12 22 11 64 90 |
After Pass 1, the largest element (90) is already in its correct position.
Pass 2
| Comparison | Action | Array |
|---|---|---|
| 34 & 25 | Swap | 25 34 12 22 11 64 90 |
| 34 & 12 | Swap | 25 12 34 22 11 64 90 |
| 34 & 22 | Swap | 25 12 22 34 11 64 90 |
| 34 & 11 | Swap | 25 12 22 11 34 64 90 |
| 34 & 64 | No Swap | 25 12 22 11 34 64 90 |
After Pass 2, the second largest element (64) reaches its correct position.
Pass 3
| Comparison | Action | Array |
|---|---|---|
| 25 & 12 | Swap | 12 25 22 11 34 64 90 |
| 25 & 22 | Swap | 12 22 25 11 34 64 90 |
| 25 & 11 | Swap | 12 22 11 25 34 64 90 |
| 25 & 34 | No Swap | 12 22 11 25 34 64 90 |
After Pass 3, the third largest element (34) reaches its correct position.
Pass 4
| Comparison | Action | Array |
|---|---|---|
| 12 & 22 | No Swap | 12 22 11 25 34 64 90 |
| 22 & 11 | Swap | 12 11 22 25 34 64 90 |
| 22 & 25 | No Swap | 12 11 22 25 34 64 90 |
Pass 5
| Comparison | Action | Array |
|---|---|---|
| 12 & 11 | Swap | 11 12 22 25 34 64 90 |
| 12 & 22 | No Swap | 11 12 22 25 34 64 90 |
The array is now completely sorted.
Summary of All Passes
| Pass | Array after Pass |
|---|---|
| Initial | 64 34 25 12 22 11 90 |
| Pass 1 | 34 25 12 22 11 64 90 |
| Pass 2 | 25 12 22 11 34 64 90 |
| Pass 3 | 12 22 11 25 34 64 90 |
| Pass 4 | 12 11 22 25 34 64 90 |
| Pass 5 | 11 12 22 25 34 64 90 |
Bubble Sort Visualization
The following illustration shows how the largest element moves towards the end of the array after every pass.
C Program for Bubble Sort
#include
int main()
{
int a[] = {64,34,25,12,22,11,90};
int n = 7;
for(int i=0;ia[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Sorted Array:\n");
for(int i=0;i
C++ Program for Bubble Sort
#include
using namespace std;
int main()
{
int a[]={64,34,25,12,22,11,90};
int n=7;
for(int i=0;ia[j+1])
swap(a[j],a[j+1]);
}
}
for(int i=0;i
Java Program for Bubble Sort
public class BubbleSort
{
public static void main(String args[])
{
int a[]={64,34,25,12,22,11,90};
for(int i=0;ia[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int x:a)
System.out.print(x+" ");
}
}
Python Program for Bubble Sort
a=[64,34,25,12,22,11,90]
n=len(a)
for i in range(n-1):
for j in range(n-i-1):
if a[j]>a[j+1]:
a[j],a[j+1]=a[j+1],a[j]
print("Sorted Array")
print(a)
Time Complexity Analysis of Bubble Sort
The efficiency of Bubble Sort depends on the number of comparisons and swaps performed during the sorting process. Since the algorithm repeatedly compares adjacent elements, its performance varies according to the initial arrangement of the data.
| Case | Time Complexity | Description |
|---|---|---|
| Best Case | O(n) | Array is already sorted (Optimized Bubble Sort) |
| Average Case | O(n2) | Array elements are randomly arranged. |
| Worst Case | O(n2) | Array is sorted in reverse order. |
Best Case Time Complexity
In the optimized version of Bubble Sort, a boolean flag is used to detect whether any swapping has occurred during a pass. If no swap takes place, it means the array is already sorted and the algorithm terminates immediately. Therefore, the Best Case Time Complexity is:
O(n)
Average Case Time Complexity
When the elements are arranged randomly, Bubble Sort performs several comparisons and swaps during each pass. On average, approximately n(n−1)/2 comparisons are required. Hence, the Average Case Time Complexity is:
O(n2)
Worst Case Time Complexity
The worst case occurs when the array is arranged in descending order and needs to be sorted in ascending order. Every adjacent comparison results in a swap, making the algorithm perform the maximum possible number of comparisons and swaps. Therefore,
Worst Case = O(n2)
Space Complexity
Bubble Sort performs sorting within the original array and requires only one temporary variable for swapping. Hence, the Space Complexity is:
O(1)
Since no additional array is required, Bubble Sort is classified as an In-place Sorting Algorithm.
Number of Comparisons
For an array containing n elements, Bubble Sort performs the following number of comparisons:
(n − 1) + (n − 2) + ... + 2 + 1
= n(n − 1) / 2
| Number of Elements | Total Comparisons |
|---|---|
| 5 | 10 |
| 10 | 45 |
| 20 | 190 |
| 50 | 1225 |
| 100 | 4950 |
Number of Swaps
The number of swaps depends on the arrangement of the input elements.
| Case | Swaps |
|---|---|
| Best Case | 0 |
| Average Case | Approximately n²/4 |
| Worst Case | n(n−1)/2 |
Advantages of Bubble Sort
- Very easy to understand and implement.
- Suitable for beginners learning sorting algorithms.
- Requires only constant extra memory.
- Stable sorting algorithm.
- Can detect an already sorted array in the optimized version.
- Works well for very small datasets.
Disadvantages of Bubble Sort
- Poor performance for large datasets.
- Performs many unnecessary comparisons.
- Requires a large number of swaps.
- Quadratic time complexity makes it inefficient.
- Much slower than Merge Sort, Heap Sort and Quick Sort.
Applications of Bubble Sort
Although Bubble Sort is rarely used in real-world software systems, it is still useful in several situations.
- Teaching sorting algorithms.
- Academic laboratory experiments.
- Interview preparation.
- Sorting very small datasets.
- Detecting nearly sorted arrays (optimized version).
- Understanding comparison and swapping techniques.
Bubble Sort vs Selection Sort
| Feature | Bubble Sort | Selection Sort |
|---|---|---|
| Method | Adjacent Swapping | Select Minimum Element |
| Stable | Yes | No |
| Adaptive | Yes (Optimized) | No |
| Swaps | Many | Very Few |
| Best Case | O(n) | O(n²) |
| Worst Case | O(n²) | O(n²) |
| Implementation | Simple | Simple |
Bubble Sort vs Insertion Sort
| Feature | Bubble Sort | Insertion Sort |
|---|---|---|
| Technique | Adjacent Swapping | Insertion |
| Stable | Yes | Yes |
| Adaptive | Yes | Yes |
| Best Case | O(n) | O(n) |
| Worst Case | O(n²) | O(n²) |
| Performance on Nearly Sorted Data | Good | Excellent |
Complexity Summary
| Property | Value |
|---|---|
| Best Case Time | O(n) |
| Average Case Time | O(n²) |
| Worst Case Time | O(n²) |
| Space Complexity | O(1) |
| Stable | Yes |
| Adaptive | Yes (Optimized Version) |
| In-place | Yes |
| Recursive | No |
Solved Example
Sort the following array in ascending order using Bubble Sort.
45 20 15 30 10
Solution
| Pass | Array |
|---|---|
| Initial | 45 20 15 30 10 |
| Pass 1 | 20 15 30 10 45 |
| Pass 2 | 15 20 10 30 45 |
| Pass 3 | 15 10 20 30 45 |
| Pass 4 | 10 15 20 30 45 |
Hence, the sorted array is:
10 15 20 30 45
AKTU Previous Year Questions
- Explain Bubble Sort with a suitable example.
- Write the algorithm and pseudocode of Bubble Sort.
- Analyze the Best, Average and Worst Case Time Complexity of Bubble Sort.
- Differentiate between Bubble Sort and Selection Sort.
- Why is Bubble Sort called a stable sorting algorithm?
- Explain the optimized version of Bubble Sort.
- Perform Bubble Sort on a given array and show each pass.
Viva Questions
- What is Bubble Sort?
- Why is it called Bubble Sort?
- Which elements are compared in Bubble Sort?
- How many passes are required for an array of n elements?
- Is Bubble Sort stable?
- Is Bubble Sort adaptive?
- What is the Best Case Time Complexity?
- What is the Worst Case Time Complexity?
- What is the Space Complexity of Bubble Sort?
- Which sorting algorithm performs fewer swaps: Bubble Sort or Selection Sort?
Interview Questions
- Why is Bubble Sort considered inefficient for large datasets?
- How can Bubble Sort be optimized?
- What is the purpose of using a swap flag?
- Can Bubble Sort detect an already sorted array?
- Is Bubble Sort suitable for linked lists? Why or why not?
- Differentiate Bubble Sort and Insertion Sort.
- Differentiate Bubble Sort and Merge Sort.
- Can Bubble Sort be implemented recursively?
- Why is Bubble Sort called an in-place sorting algorithm?
- Mention some practical applications of Bubble Sort.
Multiple Choice Questions (MCQs)
| Question | Answer |
|---|---|
| 1. Bubble Sort compares ______ elements. | Adjacent |
| 2. Bubble Sort is a ______ sorting algorithm. | Stable |
| 3. Worst Case Time Complexity is ______. | O(n²) |
| 4. Space Complexity is ______. | O(1) |
| 5. Bubble Sort is a ______ sorting algorithm. | In-place |
| 6. Largest element reaches its correct position after every ______. | Pass |
| 7. Best Case Time Complexity of Optimized Bubble Sort is ______. | O(n) |
| 8. Bubble Sort uses ______ for exchanging elements. | Swapping |
| 9. Bubble Sort is mainly suitable for ______ datasets. | Small |
| 10. Bubble Sort is based on ______. | Comparison |
Practice Questions
- Sort the array 55, 35, 25, 45, 15 using Bubble Sort.
- Explain Bubble Sort with the help of a neat diagram.
- Write C, C++, Java and Python programs for Bubble Sort.
- Compare Bubble Sort with Selection Sort.
- Explain the optimized Bubble Sort algorithm.
- Find the total number of comparisons required for an array of 8 elements.
- Explain why Bubble Sort is called a stable sorting algorithm.
- Discuss the advantages and disadvantages of Bubble Sort.
Key Takeaways
- Bubble Sort compares adjacent elements.
- The largest unsorted element moves to the end after every pass.
- Bubble Sort is a stable and in-place sorting algorithm.
- The optimized version can terminate early if the array is already sorted.
- Bubble Sort is easy to understand but inefficient for large datasets.
- It is mainly used for educational purposes and interview preparation.
Summary
Bubble Sort is one of the simplest comparison-based sorting algorithms used in Design and Analysis of Algorithms. It repeatedly compares adjacent elements and swaps them whenever they are in the wrong order. After each pass, the largest unsorted element reaches its correct position. Although Bubble Sort has a Worst Case Time Complexity of O(n2), its simplicity makes it one of the most important algorithms for beginners learning sorting techniques. The optimized version further improves performance by terminating early when the array is already sorted. Understanding Bubble Sort provides a strong foundation for learning more advanced sorting algorithms such as Insertion Sort, Merge Sort, Quick Sort and Heap Sort.