
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Bubble Sort
Bubble sort is comparison based sorting algorithm. It works by going through the list, comparing each pair of numbers, and swapping them if they are in the wrong order. This process is repeated until the list is sorted. The name "bubble sort" comes from the way smaller numbers move to the front of the list, like bubbles rising to the top.
While bubble sort is easy to understand, it is not the most efficient way to sort numbers. Other sorting methods, like insertion sort, tend to work faster and handle larger lists better.
One good thing about bubble sort is that it can tell if the list is already sorted. This can save time because it stops early if no changes are needed.
In this article, we will explain the bubble sort algorithm and show how to implement it in C++.
Working of Bubble Sort
The basic idea behind bubble sortis simple:
- Start with the first element in the array.
- Compare the current element with the next one.
- If the current element is greater than the next element, swap them.
- Move to the next element and repeat the comparison with the adjacent one.
- After each complete pass through the array, the largest element will have moved to the end.
- Repeat the process for the remaining elements (excluding the last sorted elements).
- Stop when no more swaps are needed or the array is fully sorted.
Implementation of Bubble Sort in C++
In this C++ implementation, we use the Bubble Sort algorithm to sort an array of integers in ascending order. Here's how it works:
- The BubbleSort(int A[], int n) function takes an array A[] and its size n.
- The outer loop goes through the array multiple times. Each time, the largest unsorted element "bubbles up" to its correct place.
- The inner loop compares adjacent elements and swaps them if they're not in the right order.
- After each pass, we do fewer comparisons because the largest elements are already sorted.
- If two adjacent elements are not in the right order, we swap them using a temporary variable (temp).

Example
Here is the complete code for the Bubble Sort algorithm in C++:
#include <iostream> using namespace std; void BubbleSort(int A[], int n) { // Outer loop for each pass for (int pass = n - 1; pass >= 0; pass--) { // Inner loop for comparing adjacent elements for (int i = 0; i <= pass - 1; i++) { // If elements are in the wrong order, swap them if (A[i] > A[i + 1]) { // Swap elements int temp = A[i]; A[i] = A[i + 1]; A[i + 1] = temp; } } } } int main() { int A[] = {2, 7, 1, 9, 6}; int n = sizeof(A) / sizeof(A[0]); // Display original array cout << "Original Array: "; for (int i = 0; i < n; i++) { cout << A[i] << " "; } cout << endl; // Call BubbleSort function BubbleSort(A, n); // Display sorted array cout << "Sorted Array: "; for (int i = 0; i < n; i++) { cout << A[i] << " "; } return 0; }
Here is the output of the above program. It shows the unsorted array and the array after sorting using bubble sort.
Original Array: 2 7 1 9 6 Sorted Array: 1 2 6 7 9
Implementing Bubble Sort Algorithm with Flag
The above program has a time complexity of O(n2) even in the best case. However, we can optimize it by adding an extra flag to keep track of whether any swaps are made during a pass. If no swaps occur, it means the list is already sorted, and we can skip the remaining passes to save time.
Here's how it works:
- We use a swapped flag to check if any elements are swapped during each pass.
- If no swaps are made in a pass, we stop early, as it means the list is already sorted.
- With each pass, we reduce the range of comparisons, as the largest elements are "bubbled" (moved) to their correct position at the end.
Example
In this example, we are using Bubble Sort algorithm in C++ using flag.
#include <iostream> using namespace std; void BubbleSortImproved(int A[], int n) { int pass, i, temp, swapped = 1; for (pass = n - 1; pass >= 0 && swapped; pass--) { swapped = 0; for (i = 0; i <= pass - 1; i++) { if (A[i] > A[i + 1]) { // Swap elements int temp = A[i]; A[i] = A[i + 1]; A[i + 1] = temp; swapped = 1; } } } } int main() { int A[] = {2, 7, 1, 9, 6}; int n = sizeof(A) / sizeof(A[0]); // Display original array cout << "Original Array: "; for (int i = 0; i < n; i++) { cout << A[i] << " "; } cout << endl; // Call BubbleSortImproved function BubbleSortImproved(A, n); // Display sorted array cout << "Sorted Array: "; for (int i = 0; i < n; i++) { cout << A[i] << " "; } return 0; }
When you run the program, it will display the original array and then the sorted array after applying the optimized bubble sort:
Original Array: 2 7 1 9 6 Sorted Array: 1 2 6 7 9
Time Complexity:
- Best Case: O(n) ? if the array is already sorted, the algorithm makes one pass through the array.
- Average Case: O(n2) ? when the elements are in random order.
- Worst Case: O(n2) ? if the array is in reverse order.
Space Complexity:
- O(1) ? bubble sort only requires a small amount of extra space for swapping the elements.
Conclusion
In this article, we learned about the bubble sort algorithm, which sorts numbers by swapping adjacent ones. Although it's not the fastest for large lists, we improved it by adding a flag to check if the list is already sorted. This saves time by skipping unnecessary steps when the list is already in order.