简体   繁体   English

算法 - 未排序数组中删除的时间复杂度

[英]Algorithm - the time complexity of deletion in a unsorted array

Suppose there is a unsorted array A, and it contains an element x (x is the pointer of the element), and every element has a satellite variable k.假设有一个未排序的数组A,它包含一个元素x(x是元素的指针),每个元素都有一个附属变量k。 So, we can get the following time complexity (for worst cases):因此,我们可以得到以下时间复杂度(对于最坏的情况):

If we want to Search for a particular K, then it costs O(n).如果我们想搜索一个特定的 K,那么它的成本是 O(n)。

if we want to Insert an element, then it costs O(1), because A just adds the element to the end.如果我们想插入一个元素,那么它的成本是 O(1),因为 A 只是将元素添加到最后。

What if we know x, then Delete it from the array A?如果我们知道 x,那么从数组 A 中删除它会怎样?

We have to Search for xk first and get the index of x, then Delete x via its index in A, right?我们必须先搜索xk 并获得 x 的索引,然后通过它在 A 中的索引删除x,对吗?

So for Delete , it costs O(n) too, right?所以对于Delete ,它的成本也是 O(n) ,对吧?

thanks谢谢

Finding the element with a given value is linear. 查找具有给定值的元素是线性的。

Since the array isn't sorted anyway, you can do the deletion itself in constant time. 由于数组未进行排序,因此您可以在固定时间内进行删除。 First swap the element you want to delete to the end of the array, then reduce the array size by one element. 首先将要删除的元素交换到数组的末尾,然后将数组大小减少一个元素。

Yes, that's right. 是的,这是正确的。 Also, if it's an array, deleting alone will take O(n) time because after you delete the element, you'll need to shift all the elements to the right of that element one place to the left. 此外,如果它是一个数组,单独删除将花费O(n)时间,因为删除元素后,您需要将该元素右侧的所有元素移到左侧一个位置。 So, even if you know x (for example, you will only delete the first element), it will take O(n) time. 因此,即使您知道x(例如,您只会删除第一个元素),也需要O(n)时间。

排序数组中删除操作的最坏情况时间复杂度为O(n) ,如果数组未排序,并且提到删除操作后不应更改数组的顺序,则时间复杂度将与O(n)相同否则它将是O(1)

Unsorted Array未排序数组

Ex:前任:

Items Value     [3,   5,  1,  7,  4]
Items Address   [&1, &2, &3, &4, &5]
Deleting - Value 5

1. Deletion - Order to be preserved - O(n + n) - O(2n) ~> O(n) 1. 删除 - 保留顺序- O(n + n) - O(2n) ~> O(n)

i) O(n) - Finding the position of that element (Index = 1 for value 5) i) O(n) - 查找该元素的位置(值 5 时索引 = 1)

ii) O(n) - After deleting that element, the rest of the items (1, 7, 4) needs to be reshuffled to hold the previous item's address. ii) O(n) - 删除该元素后,其余的项目 (1, 7, 4) 需要重新排列以保存前一项的地址。 Like喜欢

Items Value     [3,   1,  7,  4]
Items Address   [&1, &2, &3, &4]

2. Deletion - Without preserving the Order - O(n + 1 + 1) - O(2 + n) ~> O(n) 2. 删除 - 不保留顺序- O(n + 1 + 1) - O(2 + n) ~> O(n)

i) O(n) - Finding the position of that element (Index = 1 for value 5) i) O(n) - 查找该元素的位置(值 5 时索引 = 1)

ii) O(1) - Swape with the last element of the array. ii) O(1) - 与数组的最后一个元素交换。

Items Value     [3,   4,  1,  7,  5]
Items Address   [&1, &2, &3, &4, &5]

iii) O(1) - Delete the last element of the array. iii) O(1) - 删除数组的最后一个元素。

Items Value     [3,   4,  1,  7]
Items Address   [&1, &2, &3, &4]

Yes. 是。 It takes O(n) time to find the element you want to delete. 找到要删除的元素需要花费O(n)时间。 Then in order to delete it, you must shift all elements to the right of it one space to the left. 然后,为了删除它,您必须将所有元素向左移动一个空格。 This is also O(n) so the total complexity is linear. 这也是O(n)因此总复杂度是线性的。

Also, if you're talking about statically allocated arrays, insert takes O(n) as well. 此外,如果您正在讨论静态分配的数组,insert也需要O(n) You have to resize the array in order to accommodate the extra element. 您必须调整数组的大小才能容纳额外的元素。 There are ways to amortize this running time to O(1) though. 有一些方法可以将这个运行时间分摊到O(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二阶搜索未排序数组的时间复杂度 - Time complexity of binary search for an unsorted array 最佳情况时间复杂度检查数组是否未排序 - Best case time Complexity to check if the array is unsorted 以下算法在未排序数组中查找最小值的复杂性 - Complexity of the following algorithm to find minimum value in an unsorted array 搜索复杂度为O(log n)的算法,UNSORTED列表/数组 - Searching algorithm with complexity O(log n), UNSORTED list/array 是否可以使用 Floyd 的龟兔算法在 O(n) 时间、O(1) 空间复杂度内从未排序的数组中删除重复项? - Is it possible to remove duplicates from an unsorted array in O(n) time, O(1) space complexity, using the Floyd's tortoise and hare algorithm? 在未排序数组中插入的最佳情况和最坏情况时间复杂度 - best case and worst case time complexity for insertion in unsorted array 在未排序的数组中删除元素是否更快? - Is deletion of an element faster in unsorted array? 数组算法及其时间复杂度分析 - Analysis of array algorithm and its time complexity 特定类型数组的快速排序算法时间复杂度 - Quicksort algorithm time complexity for a particular kind of array 杂耍算法的时间复杂度(数组旋转) - Time complexity of Juggling Algorithm (array rotation)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM