Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Amortized analysis
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Dynamic array=== [[File:ArrayListAmotizedPush.png|thumb|right|270px|Amortized analysis of the push operation for a dynamic array]] Consider a [[dynamic array]] that grows in size as more elements are added to it, such as {{code|ArrayList}} in Java or {{code|std::vector}} in C++. If we started out with a dynamic array of size 4, we could push 4 elements onto it, and each operation would take [[constant time]]. Yet pushing a fifth element onto that array would take longer as the array would have to create a new array of double the current size (8), copy the old elements onto the new array, and then add the new element. The next three push operations would similarly take constant time, and then the subsequent addition would require another slow doubling of the array size. In general, for an arbitrary number <math>n</math> of pushes to an array of any initial size, the times for steps that double the array add in a [[geometric series]] to <math>O(n)</math>, while the constant times for each remaining push also add to <math>O(n)</math>. Therefore the average time per push operation is <math>O(n)/n = O(1)</math>. This reasoning can be formalized and generalized to more complicated data structures using amortized analysis.<ref name="Lecture 20" />
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)