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

  1. Compare each pair of adjacent elements.
  2. Swap them if they are in the wrong order.
  3. 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

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes