Fifth, the order table

 

 

FIG showing a basic form of the sequence table, the data storage element itself continuously, the size of each element of the memory cell share the same fixed, the standard element of its logical address and the physical address of the storage element (actual memory addresses) can be adding the logical address (i-th element) by the start address stored in the memory cell area is the product of the size (c) is calculated from

Therefore, when accessing the specified element does not need to traverse from scratch, can be obtained by calculating the address corresponding to its time complexity is O (1).

If the size of the element is not uniform , required to adopt a b FIG form of external elements , the actual data storage element separately, sequentially in the respective cell location table holding address information (i.e., links) of the corresponding element.

FIG b in this order on the index table is also referred to the actual data, which is the simplest index structure.

Structure of sequence table

A sequence of complete information table comprises two parts, a table set of elements, the other part for proper operation and to be recorded information, i.e., information about the overall situation on a table, which is part of the information including the capacity element storage area and number two elements already in the current table.

 

Table two sequences basically manner

 

 

FIG as a unitary structure, the element unit storage area for storing the table information in a continuous manner arranged in a storage area, the two portions integrally forming a complete data sequence table object.

Integrated structural integrity and strong, easy to manage. However, since a part of the data element storage area is a table object, the order table is created, a storage area on the fixed element.

Panel b is a separate structure, the object table stored in only the information related to the entire table (i.e., the capacity and the number of elements), the actual data elements stored in another element storage area in a separate, linked with the base table by associating objects.

Replace the element storage area

Integral structure due to the sequential table information storage area and the continuous data region together, the replacement of data area if you want, you can only move the whole, i.e., the entire target sequence table (refer to the configuration information storing area of ​​the sequence table) has changed.

To replace the separate structure data area, only the information table area to update the data link address area, and this order table object unchanged.

Elements of the storage area expansion

采用分离式结构的顺序表,若将数据区更换为存储空间更大的区域,则可以在不改变表对象的前提下对其数据存储区进行了扩充,所有使用这个表的地方都不必修改。只要程序的运行环境(计算机系统)还有空闲存储,这种表结构就不会因为满了而导致操作无法进行。人们把采用这种技术实现的顺序表称为动态顺序表,因为其容量可以在使用中动态变化。

扩充的两种策略

  • 每次扩充增加固定数目的存储位置,如每次扩充增加10个元素位置,这种策略可称为线性增长。

    特点:节省空间,但是扩充操作频繁,操作次数多。

  • 每次扩充容量加倍,如每次扩充增加一倍存储空间。

    特点:减少了扩充操作的执行次数,但可能会浪费空间资源。以空间换时间,推荐的方式。

 

顺序表的操作

增加元素

如图所示,为顺序表增加新元素111的三种方式

 

 

a. 尾端加入元素,时间复杂度为O(1)

b. 非保序的加入元素(不常见),时间复杂度为O(1)

c. 保序的元素加入,时间复杂度为O(n)

删除元素

 

 

a. 删除表尾元素,时间复杂度为O(1)

b. 非保序的元素删除(不常见),时间复杂度为O(1)

c. 保序的元素删除,时间复杂度为O(n)

Python中的顺序表

Python中的list和tuple两种类型采用了顺序表的实现技术,具有前面讨论的顺序表的所有性质。

tuple是不可变类型,即不变的顺序表,因此不支持改变其内部状态的任何操作,而其他方面,则与list的性质类似。

list的基本实现技术

Python标准类型list就是一种元素个数可变的线性表,可以加入和删除元素,并在各种操作中维持已有元素的顺序(即保序),而且还具有以下行为特征:

  • 基于下标(位置)的高效元素访问和更新,时间复杂度应该是O(1);

    为满足该特征,应该采用顺序表技术,表中元素保存在一块连续的存储区中。

  • 允许任意加入元素,而且在不断加入元素的过程中,表对象的标识(函数id得到的值)不变。

    为满足该特征,就必须能更换元素存储区,并且为保证更换存储区时list对象的标识id不变,只能采用分离式实现技术。

在Python的官方实现中,list就是一种采用分离式技术实现的动态顺序表。这就是为什么用list.append(x) (或 list.insert(len(list), x),即尾部插入)比在指定位置插入元素效率高的原因。

在Python的官方实现中,list实现采用了如下的策略:在建立空表(或者很小的表)时,系统分配一块能容纳8个元素的存储区;在执行插入操作(insert或append)时,如果元素存储区满就换一块4倍大的存储区。但如果此时的表已经很大(目前的阀值为50000),则改变策略,采用加一倍的方法。引入这种改变策略的方式,是为了避免出现过多空闲的存储位置。

Guess you like

Origin www.cnblogs.com/oliver3455/p/11440349.html
Recommended