Bubble sort has different time complexities depending on how sorted the input data is. This section explores its efficiency and limitations using Big O notation.
Time complexity of bubble sort
Bubble sort is one of the simplest sorting algorithms, but understanding its time complexity is important when analysing algorithmic efficiency. Time complexity tells us how the number of operations performed by the algorithm scales as the size of the input list increases. For bubble sort, the time complexity differs in best-case, average-case, and worst-case scenarios.
Best case: O(n)
The best-case time complexity of bubble sort occurs when the input list is already fully sorted in the correct order. In this situation, the algorithm performs only one full pass through the list and detects that no swaps are necessary.
Why is the best case O(n)?
Bubble sort moves through the list comparing adjacent elements.
If no swaps are required during the first pass, the list is confirmed to be sorted.
An optimised version of bubble sort checks whether any swaps were made using a flag (commonly called swapped).
If no swaps occur, the loop terminates early.
The number of comparisons made in this case is (n - 1), where n is the number of elements in the list. This results in linear time complexity, written as O(n).
Requirements for best-case performance
An optimised version of bubble sort is required, with a condition to stop early when no swaps are made.
Practice Questions
FAQ
The formula n(n - 1) / 2 represents the total number of comparisons bubble sort makes in its worst-case and average-case scenarios, where n is the number of elements in the list. This formula derives from summing the number of comparisons on each pass: (n - 1) comparisons on the first pass, (n - 2) on the second, down to 1 on the final pass. This forms an arithmetic series. The result quantifies how many comparisons bubble sort performs when it fully executes, helping to justify its O(n²) time complexity. For example, with 10 elements, the total comparisons will be 10(9)/2 = 45. This quadratic growth makes it clear how quickly the number of operations increases with list size. The significance lies in demonstrating why bubble sort becomes increasingly inefficient as n increases — it's not just the number of elements, but the number of pairwise comparisons that grows dramatically.
Bubble sort is designed to repeatedly swap adjacent elements if they are in the wrong order. This means that even small changes in position can result in multiple swaps per pass. An element might move just one position at a time per pass, requiring several swaps before it reaches its correct location. In contrast, insertion sort shifts elements and places them directly into the correct position in the sorted portion of the list. Selection sort, on the other hand, finds the minimum (or maximum) element in the unsorted portion and performs only one swap per pass to move it into place. Therefore, both insertion and selection sort minimise the number of swaps relative to bubble sort. Since swapping is a more costly operation than comparison, bubble sort’s frequent use of swaps makes it inefficient, especially for large lists. This repeated swapping of elements across multiple passes significantly adds to its overall execution time and limits its practicality.
Yes, bubble sort can be slightly improved with a simple optimisation that checks whether any swaps were made during a pass through the list. If no swaps are made in a pass, the algorithm terminates early because the list is already sorted. This optimisation introduces a Boolean flag (often called swapped) that is initially set to false at the beginning of each pass. If any swap is made during that pass, the flag is set to true. If, by the end of a pass, no swaps occurred, the flag remains false and the algorithm exits. This improvement reduces the number of unnecessary passes, particularly in the best-case scenario, resulting in a best-case time complexity of O(n). However, this optimisation does not change the average or worst-case complexities, which remain O(n²). While helpful for already or nearly sorted data, this optimisation does not make bubble sort suitable for large or highly unordered datasets.
Bubble sort handles duplicate values effectively and preserves their relative order, making it a stable sorting algorithm. A stable sort means that if two elements have the same value, their order in the original list is retained in the sorted output. In bubble sort, adjacent elements are only swapped if the left element is strictly greater than the right element. If two elements are equal, they are left in place. This approach ensures that elements with equal values are not unnecessarily moved, thus maintaining their input order. This characteristic can be important in situations where elements carry associated data — for example, sorting a list of students by score while preserving the order of students who achieved the same score. While stability may not affect the numeric correctness of sorting, it does matter in applications where secondary characteristics or original sequence must be preserved, giving bubble sort a slight advantage in cases where stability is required.
Although bubble sort is rarely used in performance-critical applications, there are a few niche scenarios where it might still be suitable or even advantageous. First, in educational contexts, bubble sort is widely used to teach basic algorithmic concepts, such as iteration, comparison, and swapping, due to its simplicity and transparency. Second, in environments with very small datasets, the difference in performance between bubble sort and more advanced algorithms becomes negligible. For example, embedded systems with limited code complexity requirements and very small data lists might use bubble sort for its straightforward implementation. Third, in cases where stability and in-place sorting are both required, and data size is trivial, bubble sort may be acceptable. Lastly, bubble sort might be used in visualisation tools to demonstrate sorting in a way that is easy to follow step-by-step, especially in animations. Despite these niche uses, bubble sort is not suitable for large-scale or performance-sensitive systems.
