1

I have the Algorithm: Input: X, a 1-D numerical array of size n

Let A = an empty 1-D numerical array of size n
For i = 0 to n-1
    Let s = X[0]
    For j = 1 to i
       Let s = s + X[j]
    End For
    Let A[i] = s /(i+1)
End For

Output: An n-element array A of numbers such that A[i] is the average of elements X[0],X[1], … ,X[i]

I am trying to write the T(n) formula and calculate it, how do write the for loop J = 1 to i within the for loop i = 0 to n-1.

what is the T(n) formula?

T(n) is time the algorithm takes to execute. t(n) will be used to compute the big-O ( O(n) ). so at the moment I have T(n) = 2n+2(n-1)+5i(n-1)+6(n-1)+1. as I counted the writes,reads, and operations in the algorithm. I don't know if the formula is write.

  • time complexity is not calculated using the reads and writes, its calculated using the number of instructions that wil get executed – Haris Oct 23 '14 at 16:41

1 Answers1

0

I am not clearly understanding your question, but still

how do write the for loop J = 1 to i within the for loop i = 0 to n-1?

The way you have written the loop is OK, its gonna do what you want..

what is the T(n) formula?

You can notice that the algorithm will make the second loop run

0 times when i=0,

1 time when i=1,

2 time when i=2, . . . and so on..

This will continue till n-1, so the complexity comes up to the sum of 0 + 1 + 2 + .... + n-1 which is n*(n-1)/2. which is asymtotically O(n^2)

Haris
  • 12,120
  • 6
  • 43
  • 70
  • T(n) time the algorithm takes to execute. t(n) will be used to compute the big-O ( O(n) ). so at the moment I have T(n) = 2n+2(n-1)+5i(n-1)+6(n-1)+1. as I counted the writes,reads, and operations in the algorithm. I don't know if the formula is write. – Z Software undergraduate Oct 23 '14 at 16:29