- 40 Algorithms Every Programmer Should Know
- Imran Ahmad
- 105字
- 2025-04-04 12:59:10
A Performance Analysis of Bubble Sort
It is easier to see that bubble sort involves two levels of loops:
- An outer loop: This is also called passes. For example, pass one is the first iteration of the outer loop.
- An inner loop: This is when the remaining unsorted elements in the list are sorted, until the highest value is bubbled to the right. The first pass will have N-1 comparisons, the second pass will have N-2 comparisons, and each subsequent pass will reduce the number of comparisons by one.
Due to two levels of looping, the worst-case runtime complexity would be O(n2).