Algorithms

How can I give an example of an algorithm that is

O(1) - bounded (by a constant)time

O(N)- linear time

O(N2) - quadratic time
quietstormAsked:
Who is Participating?
 
kirshConnect With a Mentor Commented:
Hi,

O(1) : Constant = PRINT "A"
                  PRINT "B"

O(N) : A loop = READ N
                FOR I=1 TO N DO
                     PRINT "N"

O(N2) : A loop inside a loop = READ N
                               READ L
                               FOR I=1 TO N DO
                                   FOR I=1 TO L DO
                                       PRINT "L"

This is pseudo-code of course.

Naftali Kirsh.
0
 
AxterCommented:
Is this homework?

It is against EE policy for experts to do homework questions.
We can help you with code that you have already done, but we can not give you full answers.

Please attempt to do part of the work, and then ask a specific question when you get stuck.
0
 
sateesh_babuCommented:
Just go thru some Data Structures Book, may be Horowitz & Sahani will help. You have all the answers you want there.

Babu
0
 
proskigCommented:
>How can I give an example of an algorithm that is

Yes, you can. Give us some...
0
All Courses

From novice to tech pro — start learning today.