0

I'am taking a course in complexity theory,and so it's need some mathematical background which i have a problem,. so while i'am trying to do some practicing i stuck in the bellow example

1)  for (i = 1; i < n; i++) {
2)      SmallPos = i;
3)      Smallest = Array[SmallPos];
4)      for (j = i+1; j <= n; j++)   
5)          if (Array[j] < Smallest) {
6)              SmallPos = j;
7)              Smallest = Array[SmallPos]  
            }   
8)       Array[SmallPos] = Array[i];
9)       Array[i] = Smallest;
    }

Thus, the total computing time is:

T(n)    = (n) + 4(n-1) + n(n+1)/2 – 1 + 3[n(n-1) / 2]
        = n  + 4n - 4 + (n^2 + n)/2 – 1 + (3n^2 - 3n) / 2
        = 5n - 5 + (4n2 - 2n) / 2
        = 5n - 5 + 2n^2 - n
        = 2n^2 + 4n - 5 
        = O(n^2)

and what i don't understand or confused about line 4 analyzed to n(n+1)/2 – 1, and line 5 3[n(n-1) / 2]. i knew that the sum of positive series is =n(first+last)/2 ,but when i tried to calculate it as i understand it it gives me different result. i calculate for line no 4 so it shoulb be =n((n-1)+2)/2 according to n(first+last)/2 ,but here it's n(n+1)/2 – 1. and same for 3[n(n-1) / 2].....i don't understand this too

also here's what is written in the analysis it could help if anyone can explain to me,

Statement 1 is executed n times (n - 1 + 1); statements 2, 3, 8, and 9 (each representing O(1) time) are executed n - 1 times each, once on each pass through the outer loop. On the first pass through this loop with i = 1, statement 4 is executed n times; statement 5 is executed n - 1 times, and assuming a worst case where the elements of the array are in descending order, statements 6 and 7 (each O(1) time) are executed n - 1 times.

On the second pass through the outer loop with i = 2, statement 4 is executed n - 1 times and statements 5, 6, and 7 are executed n - 2 times, etc. Thus, statement 4 is executed (n) + (n-1) +... + 2 times and statements 5, 6, and 7 are executed (n-1) + (n-2) + ... + 2 + 1 times. The first sum is equal to n(n+1)/2 - 1, and the second is equal to n(n-1)/2.

Thus, the total computing time is:

T(n)    = (n) + 4(n-1) + n(n+1)/2 – 1 + 3[n(n-1) / 2]
        = n  + 4n - 4 + (n^2 + n)/2 – 1 + (3n^2 - 3n) / 2
        = 5n - 5 + (4n2 - 2n) / 2
        = 5n - 5 + 2n^2 - n
        = 2n^2 + 4n - 5 
        = O(n^2)

here's the link for the file containing this example: http://www.google.com.eg/url?sa=t&rct=j&q=Consider+the+sorting+algorithm+shown+below.++Find+the+number+of+instructions+executed+&source=web&cd=1&cad=rja&ved=0CB8QFjAA&url=http%3A%2F%2Fgcu.googlecode.com%2Ffiles%2FAnalysis%2520of%2520Algorithms%2520I.doc&ei=3H5wUNiOINDLswaO3ICYBQ&usg=AFQjCNEBqgrtQldfp6eqdfSY_EFKOe76yg

1
  • T(n) = O(n^2) is not syntatically correct, it should be: T(n) is in O(n^2), since O(n^2) is a set of functions. Commented Oct 6, 2012 at 20:58

1 Answer 1

1

line 4: as the analysis says, it is executed n+(n-1)+...+2 times. This is a sum of (n-1) terms. In the formula you use, n(first+last)/2, n represents the number of terms. If you apply the formula to your sequence of n-1 terms, then it should be (n-1)((n)+(2))/2=(n²+n-2)/2=n(n+1)/2-1.

line 5: the same formula can be used. As the analysis says, you have to calculate (n-1)+...+1. This is a sum of n-1 terms, with the first and last being n-1 and 1. The sum is given by (n-1)(n-1+1)/2. The factor 3 is from the 3 lines (5,6,7) that are each being done (n-1)(n)/2 times

Sign up to request clarification or add additional context in comments.

6 Comments

now i understand line 4 how it's calculated ,but I've question in line 5 why a sum of n-1 terms for (n-1)+...+1. why it's not n+(n-1)+...+2,since this is in the for loop starting at 2 to n.
also in line 4 i think something is missed there must be one additional check is required which will terminate the loop.where it is.
line 5: in your first inner loop, j will assume all values for 2 to n. For each value in that loop, you will execute line 5 1 time. In the formula (n-1)+...+1 you are actually calculating how many times (in total) line 5 is executed by counting the number of times it is executed during each loop: n-1 times in the first loop (for i=1), n-2 times for i=2... If you do n+n-1+...+2 you are summing all j-values, while you want to be summing the number of times line 5 is executed.
Concerning the for loop evaluation, it is indeed true the boolean expression will be checked each time the loop is performed (when it yields true) + 1 time at the end (when it will yield false). However, in analysing complexity, every statement is considered the same (a simple increment line adds 1 to the total count, the same as an exponentiation or even more complicated things) while they are clearly not the same. Thats why, usually, we are only interested in the O(n)-nature and dont care about that 1 extra check (because breaking down each statement would leads us too far)
what you mean is for example if i've a sequence of positive numbers 1,2,3,4,5 : for i=1->j=2,3,4,5, for i=2->j=3,4,5, for i=3->j=4,5,and for i=4-> j=5 so the line 5 will be exceuted 4+3+2+1=10 times and that's matched the sequence (n-1)+...+1,on the opposite side if i considered the sequence n+n-1+...+2 ,it will be the sum of 2+3+4+5=14 times which is not true....is that what u meant.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.