954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

time complexity

What is the time complexity T(n) of the following program?
//------------------------------//
Question1:
////============///
int n, d, i, j;
cin >> n;
for (d=1; d<=n; d++)
for (i=1; i<=d; i++)
for (j=1; j<=n; j += n/10)
cout << d << " " << i << " " << j << endl;

//------------------------------
question2:
//------------------------------
void main()
{ int n, s, t;
cin >> n;
for (s = 1; s <= n/4; s++)
{t = s;
while (t >= 1)
{ cout << s << " " << t << endl;
t--;
}
}
}
//=========================//
question3:
//=========================//
void main()
{ int n, r, s, t;
cin >> n;
for (r = 2; r <= n; r = r * 2)
for (s = 1; s <= n/4; s++)
{
t = s;
while (t >= 1)
{
cout << s << " " << t << endl;
t--;
}
}
}

shamma
Newbie Poster
7 posts since Apr 2007
Reputation Points: 9
Solved Threads: 0
 

None of the programs compile, so I would guess O(1).

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

what is time complexity?

raktimranjan
Newbie Poster
1 post since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

A description of how the number of steps it takes for an algorithm changes with respect to the algorithm's input.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 
What is the time complexity T(n) of the following program? //------------------------------// Question1: ////============/// int n, d, i, j; cin >> n; for (d=1; d<=n; d++) for (i=1; i<=d; i++) for (j=1; j<=n; j += n/10) cout << d << " " << i << " " << j << endl; //------------------------------ question2: //------------------------------ void main() { int n, s, t; cin >> n; for (s = 1; s <= n/4; s++) {t = s; while (t >= 1) { cout << s << " " << t << endl; t--; } } } //=========================// question3: //=========================// void main() { int n, r, s, t; cin >> n; for (r = 2; r <= n; r = r * 2) for (s = 1; s <= n/4; s++) { t = s; while (t >= 1) { cout << s << " " << t << endl; t--; } } }


The time complexity of code written above is
Ans(1) T(n) = O(n^3) (exact time complexity=(10*(n^2)*(n+1))/2
Ans(2) T(n) =O(n^2)
Ans(3) T(n) =O((n^2)*logn)

mozaffar
Newbie Poster
2 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 
Ans(1) T(n) = O(n^3)

Wrong.(exact time complexity=(10*(n^2)*(n+1))/2

There's no such thing as 'exact' time complexity unless you define what abstract machine it's running on.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Sorry,

Answer of the 1st question will be O(n^4)

mozaffar
Newbie Poster
2 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

No, it's O(n^2).

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You