Question

Python programming.

10 points] Provide different code snippets for each of the following complexities. O(n) O( log(n))

Provide different code snippets for each of the following complexities.

O(n2)

O(n)

O(1)

O( nlog(n) )

O( log(n) )

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code Snippet For O(n2 )

  • O(n2 ) is quadratic complexity.
  • Code snippet:-

                    def printList(lst):

                           for i in lst:

                                for j in lst:

                                      print(i," ",j)

  • In the above example inner loop execute n times and outer loop executes n times.
  • So complexity is O(n2 ).

-------------------------------------------------------------

Code Snippet For O(n )

  • O(n ) is Linear complexity.
  • Code snippet:-

                    def printList(lst):

                           for i in lst:

                                      print(i)

  • In the above example loop execute n times.
  • So complexity is O(n ).

--------------------------------------------------------------------------------

Code Snippet For O(1 )

  • O(1 ) is constant complexity
  • Code snippet:-

                    def printMessage():

                          print("Hello)

  • In the above example only 1 time execute
  • So complexity is O(1 ).

-------------------------------------------------------------------

Code Snippet For O(nlogn )

  • O(nlogn ) is Linear logarithmic complexity.
  • Code snippet:-

                    def rec(lst):

                         l=len(lst)

                         if l==1:

                             return 1

                       return rec(lst[:l/2]+lst[l/2:])

  • In the above example, reach base case by dividing half the list, so it take logn complexity.
  • But in each step it divides list into half , it will take n times.
  • So total complexity is O(nlogn ).

--------------------------------------------------------------------------------------

Code Snippet For O(logn )

  • O(logn ) is logarithmic complexity.
  • Code snippet:-

                   def power2Finder(x):

                         power=0

                         while(x>1):

                              x=x/2

                              power=power+1

                       return power

  • In the above example,to find power you have to half the x
  • So complexity is O(logn ).
Add a comment
Know the answer?
Add Answer to:
Python programming. Provide different code snippets for each of the following complexities. O(n2) O(n) O(1) O(...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT