• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Time Complexity

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Marshal
Posts: 76879
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
mira pareksha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi tushar
tnx for your reply
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic