简体   繁体   中英

Time Complexity of Dynamic Arrays

I am a bit confused about time complexity of Dynamic Arrays. In this article here it states that the time complexity of insertion and deletion of Dynamic Array is O(n). I wanted to know why that is the case for insertion and deletion of dynamic array.

My understanding of why the insertion of Dynamic Array might be O(n) is because once an element is inserted the other elements need to be moved back and that is O(n). However I read somewhere else the reason for this is because in case you run out of space then extra space is reallocated the previous items copied and pasted into the new memory location.I wanted to know which reasoning is correct. Also my reasoning for an array having a time complexity of O(n) for deletion is that once an element is deleted other elements are moved forth to cover the deleted items space.However again the article gives another answer and states that since searching is O(n) in Dynamic array thus deleting is O(n) in dynamic array since an element is searched before its deleted. I would appreciate it if someone could clarify this confusion. Thanks.

My understanding of why the insertion of Dynamic Array might be O(n) is because once an element is inserted the other elements need to be moved back

Correct.

Also my reasoning for an array having a time complexity of O(n) for deletion is that once an element is deleted other elements are moved forth to cover the deleted items space.

Correct.

However I read somewhere else the reason for this is because in case you run out of space then extra space is reallocated the previous items copied and pasted into the new memory location.

I think you're confusing something here. Maybe you are thinking about what happens if you insert at the end and the array overflows. In that case you also have to copy the whole array to a new, larger location. But the cost for this can be charged to the insertions at the end that must have happened for this situation to occur. So insertion at the end is "amortized O(1)" .

The exact same reasoning works for deletion.

To summarize: Insertion/deletion at position k of an array has amortized complexity O(n - k) . In particular, insertion/deletion at an arbitrary position is O(n) and insertion/deletion at the end is O(1).

However again the article gives another answer and states that since searching is O(n) in Dynamic array thus deleting is O(n) in dynamic array since an element is searched before its deleted

Searching has nothing to due with insertion/deletion. When considering insertion/deletion cost, you usually assume that you already know the position where you want to insert/delete.

Suppose you have a dynamic array that doubles its size every time it needs to expand. Suppose you start with an array of size 1 and add 1 element. I will represent the act of writing a datum into the array with an x:

x

Now let's add a second element, forcing the array to expand and the previous 1 item to be copied:

  x
x x

The array now has size 2. I add another element, forcing resizing to 4 and copying of 2 elements: xxxxxx

I add a fourth and then a 5th element, which forces resizing again to 8:

        x
        x
    x   x
  x x   x
x x x x x

Now let me rewrite that last "tower" to spread its height across two columns. This represents a kind of amortized cost .

    x x x
  x x x x
x x x x x

The next time we need to resize is when we insert the 9th item. This time, I will represent the effort already with amortization of the copying of 8 items over the previous 8 insertions.

    x x x x x x x
  x x x x x x x x
x x x x x x x x x

In general, each doubling-and-copying operation will come on the insertion after the completion of a successive power of 2, and the number of items copied will be that power of 2, which is half the number of insertions since the last doublying-and-copying. So this means that, amortized, the average of each insertion operation requires the insertion and two copies, or a total of 3, which makes the cost 3n which is in O(n). The key point is amortization. The cost of inserting n elements into a dynamic array (that doubles its size on expansion) is bursty , but, on average , in O(n). You could think of each insertion as "paying the cost" or the "tax" of the future copies that might have to be performed when the array expands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM