Help with my big oh algorithm analaysis

  • Thread starter Thread starter mycrafish
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary
SUMMARY

The discussion focuses on analyzing the time complexity of a nested loop algorithm written in Java. The algorithm consists of two outer loops iterating over 'n', with an inner loop that executes 'n*n' times when the indices are equal, and a logarithmic loop that runs 'log2(n)' times when the indices are not equal. The established time complexity is O(n^3 + n^2 log2(n) - n log2(n)), derived from evaluating the number of iterations based on the conditions within the loops. Participants emphasize the importance of understanding how to break down the loops to accurately assess their contributions to the overall complexity.

PREREQUISITES
  • Understanding of Big O notation
  • Familiarity with nested loops in programming
  • Basic knowledge of Java syntax and control structures
  • Concept of logarithmic time complexity
NEXT STEPS
  • Study Big O notation in depth
  • Practice analyzing nested loops in various algorithms
  • Learn about time complexity analysis techniques
  • Explore Java's System.out.println() performance implications
USEFUL FOR

Students, software developers, and algorithm enthusiasts looking to deepen their understanding of time complexity analysis and improve their algorithm optimization skills.

mycrafish
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if (i == j){
for(int k = 0; k < n*n; k++){
System.out.println();
}
}
else{
for(int h = 1; h <= n; h = h*2){
System.out.println();
}
}
}​
 
Physics news on Phys.org
Any attempt at a solution? Where have you gotten so far? As a small hint, O(n) is the worst case, so look at those loops and think where the most steps can be taken.
 
i know the answer is n3 + n2log2n - nlog2n but i have no idea how to get this far
I know the loops runs n^2 times at least but I am not sure how to do the if statements
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K