-5
    int i;
    for(i=0;i<n;i++)
    {
     if(i==number);
       break;
    }

OR

    for(i=0; ;i++)
    {
    if(i==number)
    break;
    }

Does removing the comparison part in the for loop effects the time complexity or not ?

Alok Singh
  • 51
  • 1
  • 10

1 Answers1

1

You can't say which is faster precisely...

The time complexity for the first one is O(min(n, number)) and the second is O(number).


if n is bigger (or equal) than number, the first will be equal to the second.

  • first: O(number) (as number is smaller than n, min(n, number) = number
  • second:O(number)

if n is smaller than number, the first will be faster (as it also stops in n).

  • first: O(n) (as n is smaller than number, min(n, number) = n
  • second:O(number)

in an overall view, the first will be faster.

As you can see, removing the comparison inside the for look do make difference, what is quite obvious given the second case where their complexities become different.

Daniel
  • 7,357
  • 7
  • 32
  • 84