public static int HistogramMedian(this int[] values)
{
int total = 0;
// for all values
for (int i = 0; i < values.Length; i++)
{
// accumulate total
total += values[i];
}
int halfTotal = total / 2;
int median = 0;
int v = 0;
// find median value
for (; median < values.Length; median++)
{
v += values[median];
if (v >= halfTotal)
break;
}
return median;
}