Create a median formula

This is the first attempt of the Median Formula, feel free to update it.
This commit is contained in:
syr4300 2022-08-07 23:15:19 +08:00 committed by GitHub
parent 3d2cfb99b7
commit b86d99edb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,30 @@
alert(selectMedian([75,3,5,52300,5,4,0,123,2,76,768,28]));
function selectMedian(arr) {
const Length = arr.length, halfLength = Length/2;
if (Length % 2 == 1)
return select(arr, halfLength);
else
return 0.5 * (select(arr, halfLength - 1) + select(arr, halfLength));
}
function select(arr, mid) {
// Select the mid element in arr
// arr: List of numerics
// mid: Index
// return: The mid element (in numerical order) of arr
if (arr.length == 1)
return arr[0];
else {
const pivot = arr[0];
const lows = arr.filter((e)=>(e<pivot));
const highs = arr.filter((e)=>(e>pivot));
const pivots = arr.filter((e)=>(e==pivot));
if (mid < lows.length) // the pivot is too high
return select(lows, mid);
else if (mid < lows.length + pivots.length)// We got lucky and guessed the median
return pivot;
else // the pivot is too low
return select(highs, mid - lows.length - pivots.length);
}
}