The physics
A spring pulls back with a force F = kx. The force grows as the stretch x grows.
So the spring's work is the area under the F–x line. From x₁ to x₂, the spring does −½k(x₂² − x₁²). The minus sign means it pulls against the stretch.
F = kx Wspring = −½k(x₂² − x₁²) stored energy ½kx²
A squeezed spring stores ½kx². On a smooth floor, it gives all of it to a block. So ½kx² = ½mv².
A buffer can also turn part of the energy into heat. Then the spring keeps only the rest.
With gravity, count the whole fall. A block let go at the natural length falls to 2mg/k. A ball dropped from h squeezes the spring by d, with mg(h + d) = ½kd².
½kx² = ½mv² mgx = ½kx², so x = 2mg/k mg(h + d) = ½kd²