How can I calculate the complexity of a c++ code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser123
    New Member
    • Dec 2009
    • 108

    #1

    How can I calculate the complexity of a c++ code?

    Calculate the complexity of the following C++ code fragment: [3]
    count = 0;
    for (i = 1; i <= n; i *= 2)
    for (j = 1; j <= i; j++)
    2
    count++;
    Note: You are required to show all your workings.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Start here http://en.wikipedia.org/wiki/Cyclomatic_complexity.

    Comment

    • phpuser123
      New Member
      • Dec 2009
      • 108

      #3
      I managed to get the answer


      Outer loop executes log n times.
      Inner loop has cost k which doubles each time.


      This is a clue,some1 gave me but I can't understand how he arrived to log n for the outer loop.......
      Can any1 give me any indication hw,log n ws obtained ???

      Thanks

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Read this: http://en.wikipedia.org/wiki/Big_O_notation.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          There are many measures of complexity. Which one are you being asked to provide?

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            Aside: Did you calculate the final answer?
            1 + 2 + 4 + 8 + ... + 2^m = 2^(m+1) - 1
            Last edited by jkmyoung; May 10 '10, 02:02 PM. Reason: Changed letters to avoid confusion.

            Comment

            Working...