0
0
mirror of https://github.com/bobwen-dev/htpdate synced 2025-04-11 23:00:44 +02:00

Delete median.coffee

This commit is contained in:
Bob Wen 2024-12-27 04:41:48 +00:00 committed by GitHub
parent 5f09446395
commit 0e55734c23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,25 +0,0 @@
# Select the kth element in arr
quickselect = (arr, k) ->
return arr[0] if arr.length is 1
pivot = arr[0]
lows = arr.filter (e) -> e < pivot
highs = arr.filter (e) -> e > pivot
pivots = arr.filter (e) -> e is pivot
if k < lows.length
# the pivot is too high
quickselect lows, k
else if k < lows.length + pivots.length
# We got lucky and guessed the median
pivot
else
# the pivot is too low
quickselect highs, k - lows.length - pivots.length
module.exports = (arr) ->
L = arr.length
halfL = L / 2
if (L % 2) is 1
quickselect arr, halfL
else
0.5 * (quickselect(arr, halfL - 1) + quickselect(arr, halfL))