3 Replies - 359 Views - Last Post: 01 February 2013 - 05:23 PM Rate Topic: -----

#1 rick2993  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-February 13

Big O notation / algebraic formula

Posted 01 February 2013 - 05:05 PM

Hey everyone. I can't seem to figure out the big O notation or the algebraic formula as it applies to this snippet of code.

I'm assuming it is O(n^2)?

Can anyone confirm this or help me out with the algebraic forumla?
e.g. N^2 + 3n

for ( outerLoop = n - 1; outerLoop >= 0; outLoop++)
{
for (innerLoop = 0; innerLoop < outerLoop; innerLoop++)
{
if (menuList [ innerLoop ].getMenuName().compareToIgnoreCase(menuList[innerLoop + 1].getMenuName()) > 0)
{
total = menuList[inner Loop];
menuList[innerLoop] = menuList[innerLoop + 1];
menuList[innerLoop + 1] = total;
}
}
}



ahhh sorry about the looks of the code. idk how to edit so here it is w/ indents

for ( outerLoop = n - 1; outerLoop >= 0; outLoop++)
{
     for (innerLoop = 0; innerLoop < outerLoop; innerLoop++)
     {
          if (menuList [ innerLoop ].getMenuName().compareToIgnoreCase(menuList[innerLoop + 1].getMenuName()) > 0)
          {
               total = menuList[inner Loop];
               menuList[innerLoop] = menuList[innerLoop + 1];
               menuList[innerLoop + 1] = total;
          }
     }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Big O notation / algebraic formula

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,259
  • Joined: 27-December 08

Re: Big O notation / algebraic formula

Posted 01 February 2013 - 05:18 PM

There are n steps, so the series is equivalent to n(n+1)/2. There are three steps in the inner loop, so multiply by 3. So T(n) = 3/2 (n^2 + n), which is O(n^2).
Was This Post Helpful? 1
  • +
  • -

#3 rick2993  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-February 13

Re: Big O notation / algebraic formula

Posted 01 February 2013 - 05:22 PM

Thanks you!
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,259
  • Joined: 27-December 08

Re: Big O notation / algebraic formula

Posted 01 February 2013 - 05:23 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1