I understand that the speed of sound is inversely proportional to the density of the medium as shown here and as answered for this question.
The problem now is that the speed of sound in air actually decreases with altitude although the density of the air decreases. This is shown here and here.
I understand that the speed of sound also depends on the elasticity, but I'm not sure how this can change for air.
So what is actually happening? How can the speed of sound decrease although the density has also decreased?