Блог пользователя tweety

Автор tweety, история, 11 лет назад, По-английски

If I had in my algorithm something like this:

for (int i = 0; i < n; i++) if (i % 2) continue; else { code code code... }

Will the time complexity be O(n) or O(n / 2)?

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
11 лет назад, скрыть # |
 
Проголосовать: нравится +9 Проголосовать: не нравится

Strictly there are n/2 operations, but in asymptotic notation O(n) = O(n/2), so they are essentially the same (In Big O notation), because n/2 = (1/2)*n and 1/2 is a constant that is not dependent on n.

A more formal definition is to come back to the Big O definition, it means that we can find a constant k such that k*n >= n/2 for sufficiently large n. In simple words it means that n is an upper bound of n/2.