Bubble Sort

Data Structure and Algorithm 253 views Nov 05, 2025 1 min read

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
Share this Post
🚀 Want to Test Your Knowledge?

Take quizzes related to this topic and see where you stand!

Start Quiz Now
Back to Tutorials