Calculating median – javascript

Change your median method to this: function median(values){ if(values.length ===0) throw new Error(“No inputs”); values.sort(function(a,b){ return a-b; }); var half = Math.floor(values.length / 2); if (values.length % 2) return values[half]; return (values[half – 1] + values[half]) / 2.0; } fiddle

How do I find the median of numbers in linear time using heaps?

You would use a min-max-median heap to find the min, max and median in constant time (and take linear time to build the heap). You can use order-statistics trees to find the kth smallest/largest value. Both of these data structures are described in this paper on min-max heaps [PDF]. Min-max heaps are binary heaps that … Read more

Fastest way of finding the middle value of a triple?

There’s an answer here using min/max and no branches (https://stackoverflow.com/a/14676309/2233603). Actually 4 min/max operations are enough to find the median, there’s no need for xor’s: median = max(min(a,b), min(max(a,b),c)); Though, it won’t give you the median value’s index… Breakdown of all cases: a b c 1 2 3 max(min(1,2), min(max(1,2),3)) = max(1, min(2,3)) = max(1, … Read more

Calculating Median in Ruby

Here is a solution that works on both even and odd length array and won’t alter the array: def median(array) return nil if array.empty? sorted = array.sort len = sorted.length (sorted[(len – 1) / 2] + sorted[len / 2]) / 2.0 end

What is the right approach when using STL container for median calculation?

Any random-access container (like std::vector) can be sorted with the standard std::sort algorithm, available in the <algorithm> header. For finding the median, it would be quicker to use std::nth_element; this does enough of a sort to put one chosen element in the correct position, but doesn’t completely sort the container. So you could find the … Read more

Calculate median in c#

Looks like other answers are using sorting. That’s not optimal from performance point of view because it takes O(n logn) time. It is possible to calculate median in O(n) time instead. The generalized version of this problem is known as “n-order statistics” which means finding an element K in a set such that we have … Read more

How to find median and quantiles using Spark

Ongoing work SPARK-30569 – Add DSL functions invoking percentile_approx Spark 2.0+: You can use approxQuantile method which implements Greenwald-Khanna algorithm: Python: df.approxQuantile(“x”, [0.5], 0.25) Scala: df.stat.approxQuantile(“x”, Array(0.5), 0.25) where the last parameter is a relative error. The lower the number the more accurate results and more expensive computation. Since Spark 2.2 (SPARK-14352) it supports estimation … Read more

“On-line” (iterator) algorithms for estimating statistical median, mode, skewness, kurtosis?

I use these incremental/recursive mean and median estimators, which both use constant storage: mean += eta * (sample – mean) median += eta * sgn(sample – median) where eta is a small learning rate parameter (e.g. 0.001), and sgn() is the signum function which returns one of {-1, 0, 1}. (Use a constant eta if … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)