动态阵列保证澄清



在Skiena的算法设计手册中,他提到:

The primary thing lost using dynamic arrays is the guarantee that each array
access takes constant time in the worst case. Now all the queries will be fast, except
for those relatively few queries triggering array doubling. What we get instead is a
promise that the nth array access will be completed quickly enough that the total
effort expended so far will still be O(n).

我正在努力理解这一点。数组查询将如何扩展数组?

动态数组是不需要指定大小的数组(想想java中的ArrayList(。在后台,动态数组是使用常规数组实现的。但是,由于它是一个常规数组,因此 ArrayList 的实现需要指定基础数组的大小。

因此,在动态数组中处理此问题的典型方法是使用一定数量的元素初始化标准数组,然后当它达到最大元素数时,数组的大小将加倍。

由于这种底层功能,大多数时候添加到动态数组时需要恒定的时间,但偶尔它会使"引擎盖下"标准数组的大小翻倍,这将比正常的添加时间花费更长的时间。

如果你对他使用"查询">

这个词感到困惑,我相信他的意思是说"在数组中添加或删除",因为一个简单的"get"查询不应该与底层标准数组大小相关。

最新更新