Why is bubble sort O(n^2)?

后端 未结 6 1391
庸人自扰
庸人自扰 2020-12-03 10:47
for (int front = 1; front < intArray.length; front++)
{
    for (int i = 0; i  < intArray.length - front; i++)
    {
        if (intArray[i] > intArray[i +          


        
6条回答
  •  长情又很酷
    2020-12-03 11:18

    How you basically calculate N...

    • Each line: +1
    • Each Loop *N

      So you start adding numbers get to your first loop now you have N+1, you keep going and you eventually get N*N or N^2 for the time plus some number. Pulling off the number as it is generally insignificant compared to N.

    Pretty much N is a representation of all the items in the loop kind of like 1,2,3...N. So it is simply representing a number not how many times a loop, loops.

提交回复
热议问题