• Post Reply Bookmark Topic Watch Topic
  • New Topic

Time Complexity  RSS feed

 
mira pareksha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I have an Algorithm Analysis exam and I was practicing the time complexity and I found this question on the internet.
I tried to solve it but I need you to check if my solution is right or not.

sum = 0 ;
for(i=1; i<=2*n; i++)
sum = sum + 1;

my answer : O(n)

sum = 0 ;
for(i=1; i<=n*n; i++)
sum = sum + 1 ;

my answer: O(n^2)

sum = 0 :
for(i=1; i<=n; i++)
sum = sum + n ;

my answer :O(n^2)

sum = 0 ;
for(i=1; i<=n i++)
for(j=1; j<=i; j++)
sum = sum + i ;

my answer O(n^2)

sum = 0 ;
for(i=1; i<=n; i++)
for(j=1; j<=n; j*=2)
sum = sum + 1 ;

my answer : O(nlogn)
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sum = 0 :
for(i=1; i<=n; i++)
sum = sum + n ;

my answer :O(n^2)

It is O(n). . Rest are ok..
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
mira pareksha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi tushar
tnx for your reply
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!