Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it common practice to double array capacity when full?

Tags:

arrays

I've noticed that it is very common (especially in interview questions and homework assignments) to implement a dynamic array; typically, I see the question phrased as something like:

Implement an array which doubles in capacity when full

Or something very similar. They almost always (in my experience) use the word double explicitly, rather than a more general

Implement an array which increases in capacity when full

My question is, why double? I understand why it would be a bad idea to use a constant value (thanks to this question) but it seems like it makes more sense to use a larger multiple than double; why not triple the capacity, or quadruple it, or square it?

To be clear, I'm not asking how to double the capacity of an array, I'm asking why doubling is the convention.

like image 252
Caleb Brinkman Avatar asked Oct 04 '14 07:10

Caleb Brinkman


2 Answers

Yes, it is common practice.

Doubling is a good way to manage memory. Heap management algorithms are often based on the classic Buddy System, its an easy way to deal with addressing and coalescing and other challenges. Knowing this, it is good to stick with multiples of 2 when dealing with allocation (though there are hybrid algorithms, like slab allocator, to help with fragmentation, so it isn't so important as it once was to use the multiple).

Knuth covers it in one of his books that I have but forgot the title.

See http://en.wikipedia.org/wiki/Buddy_memory_allocation

Another reason to double an array size is about the addition cost. You don't want each Add() operation to trigger a reallocation call. If you've filled N slots, there is a good chance you'll need some multiple of N anyway, history is a good indicator of future needs, so the object needs to "graduate" to the next arena size. By doubling, the frequency of reallocation falls off logarithmically (Log N). Doubling is just the most convenient multiple (being the smallest whole multiplier it is more memory efficient than 3*N or 4*N, plus it tends to follow heap memory management models closely).

like image 152
codenheim Avatar answered Nov 14 '22 13:11

codenheim


The reason behind doubling is that it turns repeatedly appending an element into an amortized O(1) operation. Put another way, appending n elements takes O(n) time.

More accurately, increasing by any multiplicative factor achieves that, but doubling is a common choice. I've seen other choices, such as in increasing by a factor of 1.5.

like image 37
NPE Avatar answered Nov 14 '22 15:11

NPE