Bubble Sort Algorithm

Bubble Sort Algorithm Tutorial in C and C++ | Learn It the Easy Way [2025]

What is Bubble Sort?

Bubble Sort Algorithm: Bubble Sort is one of the most fundamental and widely recognized sorting algorithms in computer science. Its simplicity and educational value make it a popular choice for introducing beginners to the concept of algorithmic thinking and sorting mechanisms. Though not the most efficient for large datasets, Bubble Sort’s step-by-step operation provides an excellent foundation for understanding how comparison-based sorting works.

Bubble Sort Key Features

  • In-place sorting algorithm
  • Stable sorting technique
  • Works in O(n²) time complexity
  • Best sorting algorithm for small data sets

Bubble Sort Algorithm Explained

Bubble Sort goes through the list, swapping neighboring elements that are out of order, and keeps repeating this process until the entire list is sorted.

How does bubble sort work ?

  1. Start from the beginning of the array
  2. Compare each pair of adjacent elements
  3. If two neighboring elements aren’t in the correct order, simply swap them to move closer to the desired sequence.
  4. Repeat until no swaps are needed

Bubble Sort Code in C

#include <stdio.h>

void bubbleSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

Bubble Sort Code in C++

#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                swap(arr[j], arr[j + 1]);
            }
        }
    }
}

What is the best case time complexity of bubble sort?

CaseTime Complexity
Best CaseO(n)
Average CaseO(n²)
Worst CaseO(n²)
  • Space Complexity: O(1) – No extra space used
  • Best for educational purposes, interview preparation, and embedded systems sorting problems

Can bubble sorter used in alphabetical order?

we can compare strings using comparison operators like > or <, or use string comparison functions depending on the programming language.

Exmaple in C

#include <iostream>
#include <string>
using namespace std;

void bubbleSort(string arr[], int n) {
    for (int i = 0; i < n-1; ++i) {
        for (int j = 0; j < n-i-1; ++j) {
            if (arr[j] > arr[j+1]) {
                // Swap if out of order
                string temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

int main() {
    string names[] = {"banana", "apple", "grape", "cherry"};
    int n = sizeof(names)/sizeof(names[0]);

    bubbleSort(names, n);

    cout << "Sorted names:\n";
    for (int i = 0; i < n; ++i)
        cout << names[i] << endl;

    return 0;
}

Can bubble sort be used for descending order ?

Bubble sort can be easily modified to sort in descending order by just changing the comparison condition.

Key Difference:

  • Ascending order: if (arr[j] > arr[j+1])
  • Descending order: if (arr[j] < arr[j+1])

Example: Descending Order (C++)

#include <iostream>
using namespace std;

void bubbleSortDescending(int arr[], int n) {
    for (int i = 0; i < n - 1; ++i) {
        for (int j = 0; j < n - i - 1; ++j) {
            if (arr[j] < arr[j + 1]) {
                // Swap for descending order
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int numbers[] = {10, 4, 7, 1, 9};
    int n = sizeof(numbers) / sizeof(numbers[0]);

    bubbleSortDescending(numbers, n);

    cout << "Sorted in descending order:\n";
    for (int i = 0; i < n; ++i)
        cout << numbers[i] << " ";

    return 0;
}

Output:

Sorted in descending order:
10 9 7 4 1

You can do the same thing with strings for reverse alphabetical order:

if (arr[j] < arr[j+1]) // for strings

Benefits of Bubble Sort

  • Easy to implement
  • Perfect for beginner programmers
  • Helps understand the concept of element swapping and iteration
  • Frequently asked in coding interviews and technical exams

Applications of Bubble Sort

  • Embedded software systems
  • Microcontroller data sorting
  • Sorting sensor values
  • Small dataset processing in IoT

Final Thoughts

Use Bubble Sort to master the basics of sorting algorithms. With its simple logic and straightforward code, it’s ideal for learning, teaching, and solving real-world problems in C and C++.

What is Bubble Sort in C and C++

Bubble Sort is a simple sorting algorithm used in C and C++ that repeatedly compares and swaps adjacent elements if they are in the wrong order. It continues this process until the entire array is sorted in ascending or descending order.

Why is it called Bubble Sort?

It’s called Bubble Sort because the largest (or smallest) elements “bubble up” to the end (or beginning) of the array after each iteration, just like bubbles rising to the surface of water.

Can I use Bubble Sort for large datasets?

While Bubble Sort works for small arrays, it is inefficient for large datasets due to its O(n²) time complexity. For larger datasets, more advanced algorithms like Quick Sort or Merge Sort are recommended.

Is Bubble Sort stable and in-place?

Yes, Bubble Sort is a stable sorting algorithm (it maintains the order of equal elements) and it is in-place, meaning it does not require extra memory space.

You can also Visit other tutorials of Embedded Prep 

Special thanks to @embedded-prep for contributing to this article on Embedded Prep

Leave a Reply

Your email address will not be published. Required fields are marked *