[英]In an unordered array, isnt the time complexity of removing an element O(n)?
I read somewhere that it is O(1). 我在某处读到它是O(1)。 However, if we need to remove an element from an array we need to swap all the elements to the left and in the worst case wont will it need to make N swaps which makes the complexity O(n)?
但是,如果需要从数组中删除一个元素,则需要将所有元素交换到左侧,在最坏的情况下,是否需要进行N次交换,从而使复杂度为O(n)? I am confused?!
我很困惑?! Also is there a difference between an unordered and an unsorted array??
在无序数组和未排序数组之间也有区别吗?
由于数组是无序的,因此您可以用最后一个元素填充孔,然后将数组缩短1。无需将每个元素向下移动一个位置。
根据要删除元素的方式(即通过提供元素的索引或值),它是O(1)
或O(n)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.