Thread: What is the asymptotic time complexity of the following piece of code?

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    3

    What is the asymptotic time complexity of the following piece of code?

    How do you come up with the Big O notation?

    Code:
    float sum = 0 ;  for ( int i = 1; i < n ; i++) 
      {
                       sum + = A[i];                                                                          
                      }
     cout << sum;

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    This is kind of the simplest problem in this field so I can't give you much help without simply giving the answer. Here are some questions to think about:

    How long does the operation inside the loop ('sum += A[i];') take? (Is it constant time or non-constant time)

    How many times do you think the loop will run?

    If something takes 10 seconds and I do it 20 times then my total time is 200 seconds. The ever-improving speed of computers means that measuring in any unit of actual time is pretty useless though. We measure in growth rates instead. For every extra element of the array we have, how much longer will our program take to run?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about code's time complexity
    By nik in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2011, 03:21 PM
  2. Time Complexity
    By Stuart Dickson in forum C Programming
    Replies: 7
    Last Post: 07-20-2010, 03:13 AM
  3. Sorting (asymptotic) complexity problem
    By Petike in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2009, 07:15 AM
  4. Algorithmic Complexity + Asymptotic Notations
    By koodoo in forum C Programming
    Replies: 5
    Last Post: 09-03-2006, 02:25 AM
  5. Average asymptotic run time?
    By Ariod in forum C Programming
    Replies: 1
    Last Post: 08-03-2005, 06:47 PM