Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding running time

12 views
Skip to first unread message

Sophia

unread,
Apr 15, 2012, 6:53:09 AM4/15/12
to
Hi guys,


I have a problem with finding the running time of insertion sort. the following calculation is what I read in CLRS book but I don't understand why it used 'n' as 'times' in for loop, but then it used 'sigma tj'(j from 2 to n) in while loop, while 'tj' denotes the number of times the while loop test in line 5 is executed for that value of j. Here is the insertion sort I'm talking about :

http://tinypic.com/view.php?pic=25jffc6&s=5

Any help would really be appreciated.

Sophia

Dmitry Cherkassov

unread,
Apr 17, 2012, 10:19:57 AM4/17/12
to
The times column says how many times the line will be executed.
And # of executions of line 5 depends on value of j

line 5 is executed at most:

1 time for j = 2
2 times for j = 3
3 times for j = 4
...
n-1 times for j = n

summing over j gives you that sigma.

so the "while" head in line 5 will be executed \sigma_{j=2}^n{t_j}
times

Sophia

unread,
Apr 21, 2012, 1:15:43 PM4/21/12
to
Thanks Dmitry.
So the sigma is because of the sum over j?
And if we have another loop instead of 'while' for example a 'for' loop again, we would have the same thing again? I mean any loop inside of the first loop will be like the way you describe it and cause a sigma because of the iteration of 'j'?

Dmitry Cherkassov

unread,
Apr 21, 2012, 4:58:38 PM4/21/12
to
> So the sigma is because of the sum over j?
Use the right words. Sigma is just a denotation of summ operator.
Roughly speaking, yes.

> And if we have another loop instead of 'while' for example a 'for'
> loop again, we would have the same thing again? I mean any loop inside
> of the first loop will be like the way you describe it and cause a
> sigma because of the iteration of 'j'?
it's not important whether it's while or for loop. What is important
is
that if # steps of inner loop steps depends on specific iteration of
outer loop. That's what makes things little hadrer.
Sometimes walking through an algorithm with specified input does
help.

Sophia

unread,
Apr 22, 2012, 3:35:29 AM4/22/12
to
Thanks for your help, I understood completely :)
0 new messages