1. Time complexity (large o notation) and implementation of stacks using Python

Source: Internet
Author: User

1. Time complexity (large o notation):

O (1) < O (logn) < O (n) < O (Nlogn) < O (N2) < O (n3) < O (2n) < O (n!) < O (NN)

   

(1) Time complexity of commonly used data structures in Python:

The time complexity of the list built-in operations:

        

Dict time complexity for built-in operations:

      

(2) Abstract datatype (adt:abstract data type):

Common data Operations (5 types):

       Increase (insert)

       deleting (deleting)

       Change (change)

       Check (Find)

       Sort

2. The implementation of the stack:

(1) The performance of the stack:

     

(2) The operation of the stack:

    stack (): Create a new empty stack

    push (item): Add a new element item to the top of the stack

    pop (): Pop-up stack top element

    Peek (): Returns the top element of the stack

    is_empty (): Determines whether the stack is empty

    size (): Returns the number of elements in the stack

(3) Implementation stack:    

1 classStack (object):2     """implementation of the stack"""3     def __init__(self):4Self.__list= []5 6     #Add a new element item to the top of the stack7     defpush (self, item):8Self.__list. Append (item)9 Ten     #pop-up stack top element One     defpop (self): A         returnSelf.__list. Pop () -  -     #returns the top element of the stack the     defPeek (self): -         ifSelf.__list: -             returnSelf.__list[-1] -         Else: +             returnNone -  +     #determine if the stack is empty A     defIs_empty (self): at         returnSelf.__list== [] -  -     #returns the number of elements in the stack -     defsize (self): -         returnLen (self.__list) -  in  - if __name__=="__main__": tos =Stack () +S.push (1) -S.push (2) theS.push (3) *S.push (4) $ Panax Notoginseng     Print(S.pop ()) -     Print(S.pop ()) the     Print(S.pop ()) +     Print(S.pop ())

      Operation Result:

      

1. Time complexity (large o notation) and implementation of stacks using Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.