Big-Oh Notation Homework: Determine Complexity of Code

  • Thread starter Thread starter theRukus
  • Start date Start date
  • Tags Tags
    Notation
Click For Summary
SUMMARY

The complexity of the provided code is determined to be O(n²). The outer loop iterates through values of i from 0 to 2n, incrementing by 2, resulting in O(n) iterations. The inner loop executes (n-i) times, leading to a summation that approximates O(n²) as the number of executions of "a++" is counted. The analysis confirms that both loops contribute to the overall complexity, establishing that the algorithm runs in O(n²) time.

PREREQUISITES
  • Understanding of Big-Oh notation
  • Familiarity with nested loops in programming
  • Basic knowledge of summation formulas
  • Experience with algorithm analysis techniques
NEXT STEPS
  • Study the principles of algorithm complexity analysis
  • Learn about summation techniques in algorithm analysis
  • Explore examples of O(n²) algorithms
  • Investigate the differences between O(n) and O(log n) complexities
USEFUL FOR

Students studying computer science, software developers analyzing algorithm efficiency, and anyone interested in mastering Big-Oh notation for performance optimization.

theRukus
Messages
49
Reaction score
0

Homework Statement


Determine the complexity of the following code:

Code:
for (i = 0; i < 2*n; i += 2) 
{
   for (j=n; j > i; j--)
   {
       a++;
   } 
}


The Attempt at a Solution


Well.. The first for block is O( n ) because i is incremented by 2 each loop up to 2n. The second block is O( logn ) as the number of runs gets smaller as j increases.

So.. the whole algorithm is O( n log n ).

Any help is appreciated, I'm pretty bad with Big-Oh notation
 
Physics news on Phys.org
theRukus said:

Homework Statement


Determine the complexity of the following code:

Code:
for (i = 0; i < 2*n; i += 2) 
{
   for (j=n; j > i; j--)
   {
       a++;
   } 
}

The Attempt at a Solution


Well.. The first for block is O( n ) because i is incremented by 2 each loop up to 2n. The second block is O( logn ) as the number of runs gets smaller as j increases.
The thing we are counting is how many times "a++" is executed. The inner loop causes it to execute (n-i) times.

The outer loop iterates through values of i in 0,2,4,..2n-2. But the inner loop does not run at all if i>=n. So actually the inner loop only runs for values of i in 0,2,4,n-1, i is even.

Now count how many times "a++" is executed.

Ʃ (n-i) for values of i from 0 to n-1, i is even.You don't actually have to perform the summation to know it is O(n2). This is because you'll be adding something up like (n=10): 10+8+6+4+2 = 2*(5+4+3+2+1). What's the formula for adding numbers from 1..n?

Another way you could have guessed it is you've spotted the outerloop is O(n). The inner loop is also O(n) since the number of iterations is a function of (n-i). So the total number of inner loop iterations is O(n*n).For O(lg N) you need to see exponential decay in each iteration... something like the number of iterations on the inner loop decrease by a half, third, whatever in each outer loop iteration. To prove these things, you need to count.
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 26 ·
Replies
26
Views
3K
Replies
2
Views
3K