Sobes.tech
Junior — Middle

What is the worst-case time complexity of the bubble sort algorithm?

sobes.tech AI

Answer from AI

The time complexity of the bubble sort algorithm in the worst case is O(n²), where n is the number of elements in the array.

This is because, in the worst case (for example, when the array is sorted in reverse order), the algorithm must perform the maximum number of comparisons and swaps, passing through the array multiple times.

Example: for an array of 5 elements, the algorithm can make up to 10 comparisons and swaps.