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 ?
- Start from the beginning of the array
- Compare each pair of adjacent elements
- If two neighboring elements aren’t in the correct order, simply swap them to move closer to the desired sequence.
- Repeat until no swaps are needed
Bubble Sort Code in C
#include
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
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?
| Case | Time Complexity |
|---|---|
| Best Case | O(n) |
| Average Case | O(n²) |
| Worst Case | O(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
#include
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
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 1You can do the same thing with strings for reverse alphabetical order:
if (arr[j] < arr[j+1]) // for stringsBenefits 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
💛 Support Embedded Prep
If you find our tutorials helpful and want to support our mission of sharing high-quality embedded system knowledge, you can contribute by buying us a coffee. Every small contribution helps us keep creating valuable content for learners like you. ☕
Thank you for your support — it truly keeps Embedded Prep growing. 💻✨
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
- What is eMMC (Embedded MultiMediaCard) memory ?
- Top 30+ I2C Interview Questions
- Bit Manipulation Interview Questions
- Structure and Union in c
- Little Endian vs. Big Endian: A Complete Guide
- Merge sort algorithm
Special thanks to @embedded-prep for contributing to this article on Embedded Prep
Mr. Raj Kumar is a highly experienced Technical Content Engineer with 7 years of dedicated expertise in the intricate field of embedded systems. At Embedded Prep, Raj is at the forefront of creating and curating high-quality technical content designed to educate and empower aspiring and seasoned professionals in the embedded domain.
Throughout his career, Raj has honed a unique skill set that bridges the gap between deep technical understanding and effective communication. His work encompasses a wide range of educational materials, including in-depth tutorials, practical guides, course modules, and insightful articles focused on embedded hardware and software solutions. He possesses a strong grasp of embedded architectures, microcontrollers, real-time operating systems (RTOS), firmware development, and various communication protocols relevant to the embedded industry.
Raj is adept at collaborating closely with subject matter experts, engineers, and instructional designers to ensure the accuracy, completeness, and pedagogical effectiveness of the content. His meticulous attention to detail and commitment to clarity are instrumental in transforming complex embedded concepts into easily digestible and engaging learning experiences. At Embedded Prep, he plays a crucial role in building a robust knowledge base that helps learners master the complexities of embedded technologies.













