简体   繁体   中英

What does extendable array do when we re-allocate the memory for array when it's full?

I'm just encountered with this question that if we have a dynamically allocated array, it takes O(1) to do the insertion. But when the array is full, we need to re-allocate double space to the array, so copying of the old array takes O(n).

Is there any way we can make it O(1)?

I have read some articles saying about the extendable array but I don't quiet understand it. Can anyone help explain it more?

Thanks a lot.

Allocating double the space each time forms a geometric progression. This means that while some insertions (those that trigger the expansion) are very expensive, they are so infrequent that the amortized performance is still O(1). For example, inserting a billion elements only requires 30 doublings (2^30=1,073,741,824).

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