Sorting is one of the most common tasks in computer programming. Whenever we work with data—whether it’s numbers, names, or any kind of information—we often need to arrange it in order. Sorting helps make searching faster, data cleaner, and information easier to understand. From organizing student grades to arranging a contact list alphabetically, sorting is a fundamental part of programming and problem-solving.
with hands-on learning.
get the skills and confidence to land your next move.
One of the easiest and most beginner-friendly sorting techniques in C++ is Bubble Sort. This algorithm is simple to understand because it follows a very direct idea: compare two items, and if they are in the wrong order, swap them. Keep doing this until everything is in order. It’s called “Bubble Sort” because larger elements slowly move, or “bubble,” toward the end of the list with each pass, just like bubbles rising to the surface of water.
Bubble Sort is not the fastest sorting algorithm, but it’s perfect for beginners who want to understand the basic logic behind sorting. Once you master it, learning more advanced algorithms like Quick Sort or Merge Sort becomes much easier. Now, let’s explore several C++ programs that demonstrate how Bubble Sort can be implemented in different ways using predefined data.
Program 1: Basic Bubble Sort using Loops
This first program shows a simple implementation of Bubble Sort using two nested loops. It’s a direct and clear example to help beginners understand how the sorting process works step by step.
#include <iostream>
using namespace std;
int main() {
int arr[] {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "Sorted array in ascending order: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}In this program, two for loops are used. The outer loop runs through the entire array, while the inner loop compares each pair of adjacent elements and swaps them if they are out of order. After every full pass, the largest element moves to the end of the array. This continues until the entire array is sorted. For beginners, this method is a great starting point because the process is easy to follow and visualize.
Program 2: Optimized Bubble Sort
This version of Bubble Sort adds a small but important optimization. It checks if any swapping happened during a pass. If there were no swaps, that means the array is already sorted, and the program stops early instead of doing unnecessary passes.
#include <iostream>
using namespace std;
int main() {
int arr[] {5, 1, 4, 2, 8};
int n = sizeof(arr) / sizeof(arr[0]);
bool swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped)
break;
}
cout << "Optimized sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}Here, the program introduces a boolean variable named swapped. This flag checks whether any swaps occurred during a particular pass. If no swaps happen, it means the array is already sorted, so the program stops immediately. This small improvement makes Bubble Sort a bit faster and shows how programmers can optimize even simple algorithms with logical thinking.
Program 3: Bubble Sort using Recursion
This program demonstrates how Bubble Sort can be implemented using recursion. Instead of using loops, it relies on a function calling itself repeatedly to sort the array.
#include <iostream>
using namespace std;
void bubbleSortRecursive(int arr[], int n) {
if (n == 1)
return;
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
bubbleSortRecursive(arr, n - 1);
}
int main() {
int arr[] {37, 45, 29, 8, 12};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSortRecursive(arr, n);
cout << "Sorted array using recursion: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}In this version, the function bubbleSortRecursive() sorts the array by performing one full pass and then calling itself with one less element. Each recursive call reduces the array size, gradually sorting all elements. Although recursion is not the most efficient way for this problem, it helps beginners understand how recursion can be applied to repetitive tasks like sorting.
Program 4: Bubble Sort in Descending Order
So far, all the examples sorted the array in ascending order. This version sorts the elements in descending order, from largest to smallest.
#include <iostream>
using namespace std;
int main() {
int arr[] {15, 3, 27, 9, 42};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "Sorted array in descending order: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}The only difference here is the comparison condition. Instead of checking if arr[j] > arr[j + 1], it checks if arr[j] < arr[j + 1]. This small change makes the program sort the data from the largest value to the smallest. It’s a simple yet practical variation that helps beginners see how easy it is to modify sorting behavior with a single change.
Program 5: Bubble Sort for Strings
Bubble Sort can also be used to sort text or words alphabetically. This example shows how to sort a list of strings in C++.
#include <iostream>
#include <string>
using namespace std;
int main() {
string arr[] {"Zebra", "Cat", "Elephant", "Dog", "Ant"};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
string temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "Words sorted alphabetically: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}In this program, the same Bubble Sort logic is applied to strings instead of numbers. The comparison between strings is based on alphabetical order. This version proves that Bubble Sort is flexible and can handle different data types with very little change in logic.
Frequently Asked Questions (FAQ)
Many beginners have questions when learning Bubble Sort, so here are a few that can help clear common doubts.
Q1: What is Bubble Sort used for?
Bubble Sort is used to arrange elements in ascending or descending order. It’s mostly used for small datasets or educational purposes because it’s easy to understand and implement.
Q2: Is Bubble Sort efficient?
Not really. Bubble Sort has a time complexity of O(n²), which makes it slow for large datasets. However, it’s still a great way to learn the fundamentals of sorting.
Q3: Can Bubble Sort be used for characters or strings?
Yes, Bubble Sort works with numbers, characters, and even strings. You just need to adjust the comparison condition accordingly.
Q4: Why is it called “Bubble” Sort?
It’s called Bubble Sort because the largest elements gradually “bubble up” to the top of the list during each pass, similar to bubbles rising in water.
Q5: How do I make Bubble Sort faster?
You can make it slightly faster by adding an optimization check that stops the algorithm early when no swaps occur, as shown in Program 2.
Conclusion
Bubble Sort is one of the simplest algorithms every C++ programmer should learn. While it’s not the fastest sorting technique, it builds a strong foundation for understanding how sorting works. By studying Bubble Sort, you learn about loops, comparisons, and swapping logic—all of which are important concepts in programming.
In this article, we explored multiple versions of the Bubble Sort algorithm using predefined data: from basic looping, optimized versions, recursion, descending order, and even sorting words alphabetically. Try running these programs, modify the arrays, and observe how the results change. The more you practice, the more confident you’ll become in understanding how sorting and algorithms work in C++.




