The sequence table of data structure and algorithm based on Python

Source: Internet
Author: User

First, what is the sequential table:

The two basic implementation models of linear tables are:

1. The elements in the table are stored sequentially in a large contiguous storage area, so that the implemented table is called the sequential table (or continuous table). In this implementation, the order relationships between elements are naturally represented by the order in which they are stored.

2. Place the elements in the table in a series of storage modules constructed from links, so that the implemented table is called a linked table, or a linked list.

Two basic forms of the Order table:

Third, the sequential table structure:

Four, the realization way:

Five, add elements:

A. Adding elements to the end, with a time complexity of O (1)

B. Non-guaranteed addition elements (uncommon), Time complexity O (1)

C. Order-preserving elements added, time complexity O (n)

Vi. deleting elements:

A. Deleting a footer element with a time complexity of O (1)

B. Non-guaranteed element deletion (uncommon), Time complexity O (1)

C. Order-preserving element deletion, time complexity O (n)

Vii. about the list in Python: (Separate dynamic sequential table)

The list and tuple two types in Python are implemented in sequential tables with all the properties of the sequential table discussed earlier.

A tuple is an immutable type, which is an immutable sequential table, and therefore does not support any operation that alters its internal state, while others are similar in nature to the list.

The Python standard type list is a linear table with variable number of elements that can join and delete elements, maintain the order of existing elements in various operations (i.e., preserve order), and also have the following behavioral characteristics:

    • Efficient element access and update based on subscript (position), time complexity should be O (1);

      To satisfy this feature, the sequential table technique should be used, and the elements in the table should be stored in a contiguous storage area.

    • Allows any element to be added, and the identity of the Table object (the value of the function ID) does not change as the element is continually added.

      To satisfy this feature, the element store must be replaceable, and the identity ID of the list object will not change when the store is replaced, only the discrete implementation technique can be used.

In the official implementation of Python, list is a dynamic sequential table implemented using discrete technology. This is why using List.append (x) (or List.insert (list), x), or trailing inserts, is more efficient than inserting elements at a specified position.

In the official implementation of Python, the list implementation adopts the following strategy: When creating an empty table (or a very small table), the system allocates a storage area that can hold 8 elements, and when an insert operation (insert or append) is performed, the element store is replaced with a 4 times-fold storage area when it is full. However, if the table at this time is already large (the current threshold is 50000), then change the strategy, using the method of adding one times. This change of strategy is introduced in order to avoid excessive idle storage locations.

The sequence table of data structure and algorithm based on 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.