Bubble Sort
π Data Structure and Algorithm
π 70 views
π
Nov 05, 2025
β± Estimated reading time: 1 min
Bubble Sort Algorithm
Bubble Sort is a simple sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order.
Algorithm Steps
- Compare each pair of adjacent elements.
- Swap them if they are in the wrong order.
- Repeat until no swaps are needed.
Example (C++)
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]);
}
Time Complexity
- Best: O(n)
- Average/Worst: O(nΒ²)
Advantages
- Easy to understand and implement.
Disadvantages
- Inefficient for large datasets.
Conclusion
Bubble sort is mainly used for educational purposes and small datasets due to its simplicity.
π Some advanced sections are available for Registered Members
Register Now
Register Now
Share this Post
β Back to Tutorials