How much work is done by a spring when its end is moved from one position
x_{i} to another x_{f}? Because the force is not constant,
the work done is:

W = ∫_{i}^{f}
F dx

Substituting -kx for F gives W = - ∫ kx dx,

This gives: W = - ½ kx^{2}
| _{xi}
^{xf} = ½
kx_{i}^{2} - ½ kx_{f}^{2}