What's the complexity of for i: for o = i+1

前端 未结 4 1284
难免孤独
难免孤独 2020-12-22 06:52
for i = 0 to size(arr)
   for o = i + 1 to size(arr)
      do stuff here

What\'s the worst-time complexity of this? It\'s not N^2, because the seco

4条回答
  •  离开以前
    2020-12-22 07:41

    Think of it like you are working with a n x n matrix. You are approximately working on half of the elements in the matrix, but O(n^2/2) is the same as O(n^2).

提交回复
热议问题