Bubble Sort
Bubble Sort Algorithm in C++ (Complete Implementation)
C++ Bubble Sort Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
bool swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
// If no swapping occurred, array is sorted
if (!swapped) break;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
bubbleSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}Original array: 64 34 25 12 22 11 90 Sorted array: 11 12 22 25 34 64 90
Understanding Bubble Sort
This program teaches you how to implement the Bubble Sort algorithm in C++. Bubble Sort is one of the simplest sorting algorithms, making it excellent for learning sorting concepts. Although not efficient for large datasets, it's easy to understand and implement, making it a fundamental algorithm for beginners.
---
1. What This Program Does
The program sorts an array of integers using the Bubble Sort algorithm. For example:
Bubble Sort works by repeatedly comparing adjacent elements and swapping them if they're in the wrong order, "bubbling" the largest elements to the end in each pass.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Bubble Sort
Algorithm Concept
:
Visual Example
:
---
4. Function: bubbleSort()
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
bool swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
if (!swapped) break;
}
}
How it works
:
---
5. Step-by-Step Algorithm
Pass 1
(i = 0):
Pass 2
(i = 1):
Optimization
:
---
6. Understanding the Loops
Outer Loop: for (int i = 0; i < n - 1; i++)
Inner Loop: for (int j = 0; j < n - i - 1; j++)
Why n - i - 1?
:
---
7. Swap Operation
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
How it works
:
Example
:
---
8. Optimization: Early Termination
bool swapped = false;
if (!swapped) break;
// ... inner loop ...How it works
:
Example
:
---
9. Time and Space Complexity
Time Complexity
:
Space Complexity
: O(1)
Stability
: Stable
---
10. When to Use Bubble Sort
Educational Purposes
:
Small Datasets
:
Nearly Sorted Data
:
Not Recommended For
:
---
11. Important Considerations
Array Bounds
:
Optimization
:
In-Place Sorting
:
---
12. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning sorting algorithms, understanding time complexity, and preparing for more efficient sorting algorithms like Quick Sort and Merge Sort in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Bubble Sort
This C++ program is part of the "Sorting & Searching Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.