This looks correct.
In your example, your interval's length is 0.6
. In that interval, your number 2
is taking up (0.75-0.25)/0.6 = 0.5/0.6 = 10/12
of space. Your number 1
takes up (0.25-0.2)/0.6 = 0.05 = 1/12
of space, likewise your number 9
.
This sums up to 10/12 + 1/12 + 1/12 = 1
.
For better intuition, think about it like this: The problem is to determine how much space each array-element covers along an interval. The rest is just filling the machinery described in http://en.wikipedia.org/wiki/Weighted_average#Mathematical_definition .