Need help with an efficient algorithm to gather data that will be displayed in a chart. I am using c# but you can use pseudo code as a solution.
To explain I am using below sample.
Starting with zero create 10 bins on either side (positive bins and negative bins). A bin is just a container. (counter)
- bin10 (numbers 91 to 100 go in this bin)
- bin9 (numbers 81 to 90 go in this bin)
- bin8 (numbers 71 to 80 go in this bin)
and so on until
- bin2 (numbers 11 to 20 go in this bin)
- bin1 (numbers 1 to 10 go in this bin)
-
bin0 (number zero goes in this bin – the set point. Any number can be set point. I have taken zero to illustrate)
-
bin-1 (numbers -1 to -10 go in this bin)
- bin-2
and so on until
- bin-10 (numbers -91 to -100 go in this bin)
Need help for an efficient algorith for below.
int[] bins = CreateBins(bin range, number of bins on each side, setpoint)
CreateBins(10, 10, 0)
{
//??
}
FindTheRightBinAndInsertInFoundBin(value, bin[])
{
//??
}
FindTheRightBinAndInsertInFoundBin(77, bin[])
//that should basically do a bin8++ where bin8 is an index into the bin array
Update : A 2D array is fine if it does the job. (or any data structure for that matter like dictionary etc).
thank you
I don’t know what
rangeis supposed to mean – it should measure size, either of a single bin or all bins, because the actual “range” is determined by the set point, the number of bins and the size. I’m going to assume it’s bin size.edit
I think the key element here is the bin determing algorithm:
Math.Signto fix the indexing, because you effectively want 1-based indexing rather than 0 based indexing (relative to your set point).I have the edge case of the bin size elements wrong here (i.e. 10 will end up in bin 2 rather than bin 1). The fix is probably
which will ‘move’ the value one closer to 0. However you will need to test to prove this.