I use the implementation of the stack array if the stack is full and does not throw an error. I double the size of the array by copying elements, changing the stack reference and adding a new element to the stack. (I follow the book to teach me this).
What I donβt quite understand is why I should double it, why not increase it by a fixed amount, why not just increase it 3 times.
I guess this has something to do with the complexity of time or something else?
An explanation would be helpful!
, ( " ", , ) , . /, , , .
, , , , .
, , , 3 , .
, - .
. , (, , O (n) ) , O (n) . , (armortized O (1) IIRC), , ββ .
, , , . , . , , . ( - API), . , 1024, , ( ) 10 , 1024/K - , K = 3, 34 , , .
. 2 , , , 50%. , ROI , , - .
- () 100 , , ~ 100 . , 10 000 , , , 11 000 . 10 /, 10%.
2x 3x, - 3x; "" , "".
2x , [ , , ..] , , , , , .
Growth of more than 2 times will increase the average "constant" sagging; a growing smaller factor will increase the amount of storage that will be allocated and left behind. Depending on the relative estimated βcostsβ of the constant and abandoned appropriations, the optimal growth factor may be more or less, but growth factors that are in any approximation to the optimal will usually not be too much worse than the optimal growth factors. No matter what the optimal growth factor is, a 2x growth factor will be close enough to provide decent performance.