What is the Time complexity big-oh notation of the following algorithms ? Explai
ID: 3833746 • Letter: W
Question
What is the Time complexity big-oh notation of the following algorithms ? Explain
a.
int function1(int n)
{
int m =5;
for(int i = 0; i<n ; i++)
m += 1 ;
return m ;
}
b.
int function1(int n)
{
int m =5;
for(int i = 0; i<n ; i++)
m += 1 ;
for(int i =0 ;i <5 ;i++)
m +=1;
return m ;
}
c.
int function1(int n)
{
int m =0;
for(int i = 0; i<n ; i++)
{
for (int j = 0; j<n ; j++)
m += 1;
}
for(int i = 0; i<n ; i++)
{
for (int j = 0; j<10 ; j++)
m += 1;
}
return m ;
}
d.
int function1(int n)
{
int m =0;
for(int i = 0; i<n ; i++)
{
for (int j = 0; j < Math.sqrt(n) ; j++)
m += 1;
}
return m ;
}
Explanation / Answer
a) O(n). The loop runs n times.
b) O(n). The loop runs n times.
c) O(n^2). There is a nested loop the first loop runs n times and the second loop runs n times.
d) O(nn). There is a nested loop the first loop runs n times and the second loop runs n times.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.