a
n=6; cout<<n<<endl;
Constant time, O (1). This means that as n increases from 1 to infinity, the time required to complete this statement does not increase. Each time you increase n, the amount of time required does not increase.
b
n=16; for (i=0; i<n; i++) cout<<i<<endl;
Linear time, O (n). This means that as n increases from 1 to infinity, the time required to complete this statement increases linearly. Each time you increase n, the amount of extra time required from the previous one remains constant.
c
i=6; n=23; while (i<n) { cout<<i-6<<endl; i++; }
Linear time, O (n), same as example 2 above.
d
int a[ ] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}; n=10; for (i=0; i<n; i++) a[i]=a[i]*2; for (i=9; i>=0; i--) cout<<a[i]<<endl;
Linear time, O (n). As n grows from 1 to infinity, the time required to execute these operators increases linearly. The line is twice as high as Example 3, however Big O Notation does not relate to how cool the line is, it only refers to how time requirements grow. Two cycles require a linearly increasing amount of time with increasing n.
e
sum=0; n=6; k=pow(2,n); for (i=0;i<k;i++) sum=sum+k;
Create a graph of how many times sum=sum+k is performed taking into account the value of n:
n number_of_times_in_loop 1 2^1 = 2 2 2^2 = 4 3 2^3 = 8 4 2^4 = 16 5 2^5 = 32 6 2^6 = 64
As n goes from 1 to infinity, notice how the number of times we are in the loop exponentially increases. 2->4->8->16->32->64 . What happens if I include n out of 150? The number of times we are in a cycle becomes astronomical.
This is an exponential time: O(2^n) ( see here ) denotes an algorithm whose growth doubles with each additional element in the input dataset.Connect large size n at your own risk, you will wait hours or years to complete the calculation for multiple input elements.
Why don't we care?
As computer scientists, we are interested in correctly understanding BigO notation, because we want to be able to say such things with authority and conviction:
"The jim algorithm for calculating the distance between the planets takes exponential time. If we want to make 20 objects, it takes too much time, its code is crap, because I can do it in linear time."
And even better, if they do not like what they hear, you can prove it with the help of mathematics.