how to calculate time comlexity of the given algorithm code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • humsafar
    New Member
    • Nov 2008
    • 5

    how to calculate time comlexity of the given algorithm code

    #include<iostre am>
    #include<stdlib .h>
    using namespace std;

    int main(){
    int i, j, n;
    for(i=0;i<n; i++){

    for(j=0; j<n; j++){
    cout<<"my time complexity is = "<<i*j<<end l;
    }
    cout<<"complexi ty is increasing"<<j< <endl;
    }
    system("pause") ;
    return 0;
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    For any value of n >= 0 your outer loop body runs n times and your inner loop
    body runs n*n times so O(n^2) is the big-Oh value.

    kind regards,

    Jos

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Of course, if you put this code into your compiler and run it, you have no idea what will happen, since you never give a value to n.

      In general, most loops will have a complexity of O(n) where n is the difference between the initial value and the final value of the index. Most for loops start at 0 or 1 and end at some variable value like n, or size, or so on.

      Be careful, though, as some loops are tricky. The following loop, for example, is not O(n) but in fact O(log n):

      Code:
      for (int i = myList.size(); i > 0; i/=2) {
         // ...
      }
      Last edited by Ganon11; Nov 8 '08, 04:28 PM. Reason: No one likes infinite loops.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Ganon11
        Be careful, though, as some loops are tricky. The following loop, for example, is not O(n) but in fact O(log n):

        Code:
        for (int i = myList.size(); i >= 0; i/=2) {
           // ...
        }
        If there's no 'break' or 'return' in the body of that loop it actually is a 'dynamic halt',
        i.e. it never ends, it is O(infinity) ;-)

        kind regards,

        Jos

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Well, if you treat i as a floating point number - then it would eventually get to 0.5, 0.25, 0.125, 0.0625, etc... which is an infinite series converging to 0. However, because C/C++ get easily confused with floating points and ints, they say, "Meh, with ints, 1/2 is 0."

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Or I could realize that I said i >= 0, and 0/2 is 0...

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Ganon11
              Or I could realize that I said i >= 0, and 0/2 is 0...
              :-) yep, that was what I was pointing at.

              kind regards,

              Jos

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                The OP didn't actually ask a question, except in the title of the post. By "time complexity" did you mean big-oh, such as O(n^2)? All of the replies were based on inspection of the source code; do you need to obtain an answer by executing the code?

                Comment

                Working...