Fast algorithm for repeated calculation of percentile?

前端 未结 6 751
余生分开走
余生分开走 2020-12-12 17:21

In an algorithm I have to calculate the 75th percentile of a data set whenever I add a value. Right now I am doing this:

  1. Get value x
  2. Inse
6条回答
  •  死守一世寂寞
    2020-12-12 17:51

    If you have a known set of values, following will be very fast:

    Create a large array of integers (even bytes will work) with number of elements equal to maximum value of your data. For example, if the maximum value of t is 100,000 create an array

    int[] index = new int[100000]; // 400kb
    

    Now iterate over the entire set of values, as

    for each (int t : set_of_values) {
      index[t]++;
    }
    
    // You can do a try catch on ArrayOutOfBounds just in case :)
    

    Now calculate percentile as

    int sum = 0, i = 0;
    while (sum < 0.9*set_of_values.length) {
      sum += index[i++];
    }
    
    return i;
    

    You can also consider using a TreeMap instead of array, if the values don't confirm to these restrictions.

提交回复
热议问题