What is the order of magnitude of the following code-part (assume that \"n\" den
ID: 3669443 • Letter: W
Question
What is the order of magnitude of the following code-part (assume that "n" denotes the number of input data elements)? For (int j = 0; j<20; j++){ For (int i = 0; i<20; i++){ System.out.println( n ); } } Answer: O( ) EXPLAIN Answer. What is the order of magnitude of the following code-part (assume that "n" denotes the number of input data elements)? For (int j = 0; j<20; j++){ For (int i = 0; i<20; i++){ System.out.println( n ); } } Answer: O( ) EXPLAIN Answer.What is the order of magnitude of the following code-part (assume that "n" denotes the number of input data elements)? For (int j = 0; j<20; j++){ For (int i = 0; i<20; i++){ System.out.println( n ); } } Answer: O( ) EXPLAIN Does it matter that j<20 and i<20? DOes thgis mean it is O(1) or O(n^2) magnitude?
Explanation / Answer
Given code segement
For (int j = 0; j<20; j++)
{
For (int i = 0; i<20; i++)
{
System.out.println( n );
}
}
Answer:
O(1), where c is constant , 20*20=400 constant time for computers it is taken as constant time as it is constant value.
EXPLAIN
The first for loop that run for 20 times and inner for loop that runs for 20 times , the total number of repeatations
are done in constant time, O(1).
EXPLAIN Does it matter that j<20 and i<20? DOes this mean it is O(1) or O(n^2) magnitude?
Yes. Matters. The conditional statements j<20 and i<20 are conditions that are checked for n=20
times in constat time , O(1) . If the j<n and i<n , then the time complexity becomes, O(n^2)
since the time complexity is depends on the variable , n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.