Does this work better?
I used a high number like 58 for the first one. Then all the other values were lower and it seemed that it sorted it correctly. This is selection sort. I believe. I wrote it below with the variables above it . You should just make it a function it would be more appropriate I think. Well let me know if that is what you wanted.
Line 35 heights[i+1] = key;
The value key will be placed into the lowest element of the array when i+1 == 0. In order for that to be true, i must be equal to -1.
That cannot currently happen. The while loop at line 32 ends when i equals 0.
Solution, change line 32 to while (i>=0 && heights[i] > key)