Big-Oh Notation Homework: Determine Complexity of Code

  • Thread starter theRukus
  • Start date
  • Tags
    Notation
In summary, the complexity of the given code is O(n^2) as determined by counting the number of times "a++" is executed and taking into account the nested loops. The inner loop runs (n-i) times for each even value of i, and the outer loop iterates through even values of i up to 2n-2. This can be simplified to 2*(1+2+...n), which is equivalent to O(n^2). The code is not O(lg n) as there is no exponential decay in the number of inner loop iterations.
  • #1
theRukus
49
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
  • #2
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:

Related to Big-Oh Notation Homework: Determine Complexity of Code

1. What is Big-Oh notation and why is it important for determining the complexity of code?

Big-Oh notation is a mathematical notation used to describe the time or space complexity of an algorithm. It is important because it allows us to analyze and compare the efficiency of different algorithms in terms of the input size.

2. How do you calculate the time complexity of a code using Big-Oh notation?

To calculate the time complexity, you need to count the number of operations performed in the algorithm and then express it as a function of the input size. The highest order term in the function will represent the time complexity in Big-Oh notation.

3. What is the difference between best-case, worst-case, and average-case time complexity?

Best-case time complexity refers to the minimum number of operations required for an algorithm to complete when given the most favorable input. Worst-case time complexity refers to the maximum number of operations required for an algorithm to complete when given the most unfavorable input. Average-case time complexity refers to the expected number of operations required for an algorithm to complete when given a random input.

4. Are there any limitations to using Big-Oh notation to determine the complexity of code?

Yes, Big-Oh notation only considers the asymptotic behavior of an algorithm, meaning it only looks at the trend of the time or space complexity as the input size grows. It does not take into account other factors such as the hardware or programming language used, which can also affect the actual performance of the algorithm.

5. How can Big-Oh notation be useful in real-world applications?

In real-world applications, Big-Oh notation can help in selecting the most efficient algorithm for a given task. It can also help in identifying bottlenecks and areas for optimization in existing code. Additionally, it can aid in predicting the performance of an algorithm as the input size increases, allowing for better scalability and planning.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
26
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top