Why Bubble sort complexity is O(n^2)?

后端 未结 2 1403
慢半拍i
慢半拍i 2021-01-24 23:19

As I understand, the complexity of an algorithm is a maximum number of operations performed while sorting. So, the complexity of Bubble sort should be a sum of arithmmetic progr

2条回答
  •  忘掉有多难
    2021-01-24 23:29

    This is because big-O notation describes the nature of the algorithm. The major term in the expansion (n-1) * (n-2) / 2 is n^2. And so as n increases all other terms become insignificant.

    You are welcome to describe it more precisely, but for all intents and purposes the algorithm exhibits behaviour that is of the order n^2. That means if you graph the time complexity against n, you will see a parabolic growth curve.

提交回复
热议问题