analysis of algoritms

Almar Klein almar.klein at gmail.com
Thu Sep 9 17:54:29 EDT 2010


On 9 September 2010 23:39, Baba <raoulbia at gmail.com> wrote:

> Hi
>
> In below code "the outer loop test in step 4 will execute ( n + 1 )
> times (note that an extra step is required to terminate the for loop,
> hence n + 1 and not n executions), which will consume T4( n + 1 )
> time." (from http://en.wikipedia.org/wiki/Analysis_of_algorithms)
>
> 1    get a positive integer from input
> 2    if n > 10
> 3        print "This might take a while..."
> 4    for i = 1 to n
> 5        for j = 1 to i
> 6            print i * j
> 7    print "Done!"
>
> Why does step 4 execute n+1 times? what is the exta step mentioned
> above
>

Because it does two things:
  - increase i
  - check whether i is still smaller than n. If it is not, the loop is
terminated

Still, this is quite a simplification of what's really going on...

  Almar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100909/fef7e253/attachment-0001.html>


More information about the Python-list mailing list