There have been other questions about bubble sort time complexity, but this question is different. Everyone says that bubble sort worst case is O(n^2). In the bubble sort after i iterations of the list, the last i elements of the list are in order and don't need to be ever touched or compared again. The time complexity would only be O(n^2) if you needlessly ran over the final elements again and again.
Given that a major feature of the bubble sort is that the elements after (input size minus iteration) never need to be compared again, because it's in its correct place, why is bubble sort time complexity said to be that for something that to me I didn't think was bubble sort? Even in Wikipedia it says the time complexity is O(n^2), and then only halfway into the article it mentions that it can be "optimised" to take only about 50% of the time by not unnecessarily comparing the last i elements.
I was reminded of this because I was making a loop which checked collisions of all my objects in the world, and the pattern was that I checked:
for (int i = 0; i < numberofobjects - 1; i++)
{
{
for (int iplusone = i + 1; iplusone < numberofobjects; iplusone++)
// check collision between i and iplusone
}
}
With 400 objects a time complexity of O(n^2) would be 400 * 400 = 160,000. However it only did 79,800 comparisons, roughly 50%, which is exactly what Wikipedia said. This reminded me of the bubble sort so when I checked I was surprised to see everyone saying it was O(n^2).
Does this mean that whenever someone refers to the bubble sort they're referring to the version that needlessly reiterates over the final elements that have already been sorted? Also when different algorithms are compared bubble sort always fares the worse, but is the writer referring to the obviously bad n^2 version?