CSCI311
More on Time

More examples for calculating T(n) [to ultimately get O(n)]

Consider times of "inner parts"

notice it is i that is changing in the summation, not n.

This is why we can bring n "out".

In general, i=1 to n   c*i = c[i=1 to n   i]

Another useful fact:

Obviously, i=1 to n   1 =   n

In general, i=a to n   1 =   n - a + 1

specifically: i=2 to n   1 =   n - 2 + 1 = n - 1

Best, Worse and Average Operation Counts

Section 2.3.3

An example:

This program finds the minimum value and then returns (writes out) the rest of the array. (this is not in Java; I am using it only to consider time performance)

procedure TrailMin<p> Read(Entry[1])<p> Mindex = 1<p> for i=2 to n do<p> Read(Entry[i])<p> if Entry[i] < Entry[Mindex]<p> then Mindex = i<p> for i=Mindex + 1 to n do<p> Write(Entry[i])<p> Consider Read, Write, if, assignment all to have time 1

T(n) = 2 + i=2 to n   2 + i=Mindex+1 to n   1

= 2 +2(n - 2 + 1) + (n - (Mindex + 1) + 1)

= 2 + 2n - 4 + 2 + n - Mindex = 3n - Mindex

Clearly, Mindex may be a function of n itself.

(Remember, Mindex is the index of the minimum - not the calculations to get there)

Consider

*Worst case analysis

*Statistically average case analysis

O(?) for both?


Compare (in general, when we say log in this course we will mean log2 - whether we say so explicitly or not. SO
I strongly suggest using values of n that are powers of 2 to see the patterns since log2 are integers for powers of 2):

n2     n     nlog2n     log2n     n-1     n/log2n     2log2n (that was mean of me - look here to help with logs)

Actually we will look at these later in Chapter 3 (Tables and Figures of text there help)