DSA Selection Sort Algorithm
Last modified - 28-04-2025
Author - Krishna Shinde
When you begin your journey in Data Structures and Algorithms (DSA), You will encounter various sorting algorithms one of which is the Bubble Sort Algorithm, we have already written a detailed article on Selection Sort Algorithm . In this article, we will deep dive into the Bubble Sort data structure concept and provide codes of the Bubble Sort algorithm in C++ and Bubble Sort C program to help you understand it better.
What is Bubble Sort?
Bubble Sort is a simple comparison-based sorting algorithm. It repeatedly iterates the array, compares an element with its next element, and swaps them if the first element is greater than the next element. This process is repeated until the array becomes sorted. The largest element “bubbles up” to its correct position with each pass, which is where the name comes from.
Time Complexity of Bubble Sort
Case | Time Complexity | Space Complexity | Stability |
---|---|---|---|
Best Case | O(n) | O(1) | Stable |
Average Case | O(n²) | O(1) | Stable |
Worst Case | O(n²) | O(1) | Stable |
How Bubble Sort Works (Step-by-Step)
Here’s a step-by-step explanation of the Bubble Sort algorithm:
Bubble Sort Algorithm C++ Code
Here is a clean and optimized Bubble Sort algorithm in C++:#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(arr[j], arr[j+1]); swapped = true; } } if (!swapped) break; // Stop if array is already sorted } } void printArray(int arr[], int size) { for (int i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; } int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr)/sizeof(arr[0]); bubbleSort(arr, n); cout << "Sorted array: "; printArray(arr, n); return 0; }
Bubble Sort C Program
Here’s a simple C program of Bubble Sort Algorithm:
#include <stdio.h> void bubbleSort(int arr[], int n) { for (int i = 0; i < n-1; i++) { int swapped = 0; 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 = 1; } } if (swapped == 0) break; } } void printArray(int arr[], int size) { for (int i=0; i < size; i++) printf("%d ", arr[i]); printf(" "); } int main() { int arr[] = {5, 1, 4, 2, 8}; int n = sizeof(arr)/sizeof(arr[0]); bubbleSort(arr, n); printf("Sorted array: "); printArray(arr, n); return 0; }

Dry Run Example of Bubble Sort
Let's take a dry run of the Bubble Sort technique with the array: [5, 1, 4, 2, 8].
First pass:
Second pass:
Third pass:
No swaps in the third pass, the array is now sorted.
When to Use Bubble Sort?
Bubble sort is not efficient for large datasets, but can be used in scenarios like:
1. Teaching and learning basic DSA concepts.
2. Sorting very small or nearly sorted datasets.
3. Situations where code simplicity matters more than performance.
In professional coding interviews, understanding the workings of Bubble Sort and being able to optimize it shows strong foundational knowledge.
Advantages and Disadvantages of Bubble Sort
Advantages:
Disadvantages:
Conclusion
Bubble Sort is a simple, easy-to-understand sorting algorithm that cannot be used for large datasets, but knowing how to optimize and implement bubbles can increase your fundamental knowledge. We have already written an article on selection sort in data structures and algorithms blogs. Follow Algoflame for more programming content.