javascript - Algorithm for equal division of values -


i need algorithm make equal division of array of values. demonstrate in javascript language can applied.

i have array of values ​​with random quantity , values ex:

[5,9,14,18,19,29,49,59,89,99,129,159,189,199,259,299,599] 

(one feature i've noticed values ​​grow exponentially)

i need divide array fixed number of groups, have close quantity of items , difference of first item last 1 not large between groups. in case, divide 4 groups.

the problem: if divide 4 groups of equal quantities, may occur first item in group smaller last in relation other groups. if divide 4 groups equal ranges of values, 1 group may have few items relative others.

i want somewhere in middle, have not got solution yet.

example of acceptable solution:

[     [5,9,14,18,19,29],     [49,59,89,99],     [129,159,189,199],     [259,399,599], ] 

if did not understand question, comment.

application: filter shows ranges of values. appears this:

  • less 19
  • from 29 89
  • from 99 159
  • from 189 259
  • above 399

what can't show this:

  • from 29 399 (very large interval)

thank help!


large differences - jump small value large value

another possible example work:

[1, 2, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 19, 20, 21, 22, 25, 32, 33, 34, 35, 36, 38, 39, 40, 48, 49, 50, 55, 56, 57, 60, 61, 62, 70, 72, 73, 74, 75, 80, 100, 150, 300] 

and

[830, 1790,1990,1990,2390,2420,2599,2690,2890,3124,3340,3404,3460,3497,3590,3590,3598,3620,3690,3725,3725,3774,3780,3798,3865,3885,3890,3929,3949,3990,3990,3998,4080,4099,4128,4132,4183,4190,4199,4250,4290,4290,4290,4420,4490,4496,4507,4590,4599,4680,4750,4790,4790,4890,4898,4990,4999,5190,5299,5390,5396,5530,5590] 

this approximation logarithmic view of data , group in equal intervals.

it works calculating index of result set pushing values.

in test, whole number, first value gets index 0 , last, number smaller wanted parts (for that, take factor smaller one, because 1 move last item outside of wanted length of array).

var data = [5, 9, 14, 18, 19, 29, 49, 59, 89, 99, 129, 159, 189, 199, 259, 299, 599],      parts = 5,      smallerthanone = 0.9999999999999999,      interval = math.log(data[data.length - 1] / data[0]),      test = data.map(function (a) {          return parts * smallerthanone * math.log(a / data[0]) / interval;      }),      grouped = data.reduce(function (r, a) {          var = math.floor(parts * smallerthanone * math.log(a / data[0]) / interval);          r[i] = r[i] || [];          r[i].push(a);          return r;      }, []);      console.log(grouped);  console.log(test);
.as-console-wrapper { max-height: 100% !important; top: 0; }


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -