jdh1231 0 Newbie Poster

I have a question about finding the running time algorithm calculatino in worst case.

the code I have is

      function problem1(n);
   1.     sum = 0;
   2.     for i :=1 to n-1 do
   3.         for j = 1 to i
   4.             for k = j+1 to n do
   5.     sum++
   6.  return(sum);

Cost is 1 for all steps.
I am suppose to find the runtime of sum++.
Now, my understanding is start from the inside to outside. However, since there are three for loops going on, this confuses me very much.
What I think I have so far is line1 has time of 1, line2 = n, line3 = ∑(from i=1 to n), line4 = ∑(ti), line5 = (n-3)
sum++ equals T(n) = 1 + n + ∑(from i=1 to n) + ∑(t of i) + (n-3)

Am I doing this correct?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.