更恰当地说是将O(1)与O(n)摊销即可插入未排序的动态数组? [英] More appropriate to say Amortized O(1) vs O(n) for insertion into unsorted dynamic array?

查看:122
本文介绍了更恰当地说是将O(1)与O(n)摊销即可插入未排序的动态数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这属于stackoverflow.com/help/on-topic的软件算法下,在这种情况下,这是一种将项目添加到动态未排序数组的软件算法

This falls under "a software algorithm" from stackoverflow.com/help/on-topic, in this case, a software algorithm to add an item to a dynamic unsorted array

这是我们在课堂上制作的有关不同数据结构上的操作运行时间的图表

This is chart we made in class about the runtimes of operations on different data structures

我的问题是有关在动态未排序数组中插入(或添加)值的运行时。
这是执行此操作的代码

The question I have is about the runtime for inserting(or adding) a value into the dynamic unsorted array. Here is our code for doing this

 public void insert(E value) {
    ensureCapacity(size + 1);
    elementData[size] = value;
    size++;
}
  private void ensureCapacity(int capacity) {
    if (capacity > elementData.length) {
        int newCapacity = elementData.length + 100;
        if (capacity > newCapacity) {
            newCapacity = capacity;
        }
        elementData = Arrays.copyOf(elementData, newCapacity);
    }
}

我知道如何将其解释为O(n )。从技术上来说,suresureCapacity函数是由插入操作和运行时分析操作组成的, https://academics.tjhsst.edu/compsci/CS2C/U2/bigoh.html ,您会说两个分支的最坏情况是将原始数组的每个元素都复制到新数组中这是O(n)运算。因此,整个函数中最糟糕的情况还是最糟糕的是O(n)

I understand how this could be interpreted as O(n). The ensureCapacity function is technically apart of the operations that consist of insert and then with runtime analysis, https://academics.tjhsst.edu/compsci/CS2C/U2/bigoh.html, you would say that the worst case of the two branches is when every element of the original array is copied into the new array which is an O(n) operation. So the worst case or big oh of the whole function is O(n)

是否也可以为O(1)摊销一次参数(什么是算法的摊销分析?),因为每次调整大小时,您都必须等待特定的下一次调整大小之前需要多少时间?

Could an argument be made for amortized O(1) time as well(What is amortized analysis of algorithms?) because each time you resize, you have to wait a specific amount of time before the next resize?

在那个图表中,O(1)也有意义吗?

In that chart, would O(1) also make sense then?

推荐答案

否。

摊销O(1)时间表示非常具体的内容-表示插入 n 的成本项一次为O(n)。仅仅说花费很长时间的事情很少发生是不够的-实际上,您实际上必须进行数学分析。

"Amortized O(1) time" means a very specific thing - it means that the cost of inserting n items, one at a time, is O(n). It is not enough to say that "the things that take a long time don't happen very often" - you do actually have to analyze the algorithm mathematically.

大小写(将一项插入数组,如果已满则调整大小)是一种众所周知的情况。事实证明,如果您通过恒定的 factor 调整数组的大小(例如,每次数组加满时将其加倍),则此操作将摊销O(1)。如果您添加固定数量的元素(例如,每次添加100个元素,则该元素仍将摊销O(n),因为这需要O(n 2 )时间分别添加 n 个元素。

This particular case (inserting an item into an array, or resizing it if full) is a well-known one. As it turns out, if you resize the array by a constant factor (e.g. doubling it each time it's full) then this operation is amortized O(1). If you add a fixed number of elements (e.g. adding 100 each time it's full) then it's still amortized O(n), because it takes O(n2) time to add n elements individually.

这篇关于更恰当地说是将O(1)与O(n)摊销即可插入未排序的动态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆