1

How do you compute the number of operations each line of code would take.

Example.

Algorithm find2D (A,x)
    arrLength = A.length
    for j <- 1 to arrLength – 1 do
         for k <- 1 to arrLength – 1 do
                if A[j][k] = x then
                  return true
          increment k
    increment j
return false

I came up with this pseudo code. So I'm not really sure how to count the number of operations each line of code takes.

So like the first loop would be 1+n operations since you have to set the j, compare j to arrLength - 1, and it will loop n-1 times. So that gives you n-1 + 1 + 1 which is n+1 operations.

So for the second loop would it just be the same thing even though its nested.

I'm a little confused on the A[j][k] = x comparison, how many operations would that be.

Thanks.

Matt
  • 7,049
  • 7
  • 50
  • 77
  • Comparison is usually considered 1 operation, and that comparison will be called n^2 times (worst case) since you are comparing every item in the array with every other item. – Lithium Jan 20 '11 at 21:28

3 Answers3

2

The general rule of thumb is this:

For each time you loop over every element in an array, multiply by N

For each time you divide the array in two repeatedly, multiply by log(N)

Since you have two nested loops over the whole array, you're O(N^2).

Calculating the constant factor is trickier, and is dependent on details of the language, compiler, and hardware. Basically just have to work that out empirically.

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84
1

Big-Oh is asymptotic, so you shouldn't be concerned with the "1" in "1+n".

If all you're looking for is the Big-Oh classification of that code, simply consider that you have two loops, one nested inside the other, and each loop does some constant number of operations per element. Then the inside loop is O(n), and the outside loop is executing the inside loop n times, so the outside loop is O(n^2). Then the whole function is O(c+n^2) where c is the constant number of operations from the code surrounding the outside loop. Since Big-Oh is asymptotic, the c disappears and your function is O(n^2).

If you're trying to calculate the exact number of operations the code takes, you'll probably need to establish what abstract machine you're using.

Hope this helps.

William
  • 1,993
  • 2
  • 23
  • 40
0

In terms of big O, you should choose the operation you're going to count, most frequent and atomic one. In this case it is the comparison inside both loops. You don't need to count how many comparisons loops are doing to iterate, you just count how many times the innermost part will be executed in worst case. It will be n-1 for outer loop, each of n-1 for inner loop, that gives O(n^2) total.

NOtherDev
  • 9,542
  • 2
  • 36
  • 47