Skip to content

Commit

Permalink
Don't assume that partialsort! partitions around the selected index
Browse files Browse the repository at this point in the history
  • Loading branch information
brenhinkeller committed Jun 13, 2024
1 parent 29ed673 commit b1c2ca7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/Sorting/nanmedian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,20 @@ function _nanmedian!(A::AbstractArray{T}, ::Colon) where {T}
n < 2 && return float(T)(A[iₗ])
= (iₗ + iᵤ) ÷ 2
if iseven(n)
if n < 384
Aᵢ₋, Aᵢ₊ = if n < 384
quicksort!(A, iₗ, iᵤ)
A[i½], A[i½+1]
else
quickselect!(A, iₗ, iᵤ, i½)
quickselect!(A, i½+1, iᵤ, i½+1)
quickselect!(A, iₗ, iᵤ, i½), quickselect!(A, iₗ, iᵤ, i½+1)
end
return (A[i½] + A[i½+1]) / 2
return float(T)(0.5*Aᵢ₋ + 0.5*Aᵢ₊)
else
if n < 192
quicksort!(A, iₗ, iᵤ)
else
quickselect!(A, iₗ, iᵤ, i½)
end
return A[i½] / 1
return float(T)(A[i½])
end
end

Expand Down
10 changes: 5 additions & 5 deletions src/Sorting/nanpctile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ function _nanquantile!(A::AbstractArray{T}, q::Real, ::Colon) where {T}
iₚ = q*n₋ + iₗ
iₚ₋ = floor(Int, iₚ)
iₚ₊ = ceil(Int, iₚ)
if n₋ < 384
f = iₚ - iₚ₋
Aᵢ₋, Aᵢ₊ = if n₋ < 384
quicksort!(A, iₗ, iᵤ)
A[iₚ₋], A[iₚ₊]
else
quickselect!(A, iₗ, iᵤ, iₚ₋)
quickselect!(A, iₚ₊, iᵤ, iₚ₊)
quickselect!(A, iₗ, iᵤ, iₚ₋), quickselect!(A, iₗ, iᵤ, iₚ₊)
end
f = iₚ - iₚ₋
return f*A[iₚ₊] + (1-f)*A[iₚ₋]
return f*Aᵢ₊ + (1-f)*Aᵢ₋
end

# Generate customized set of loops for a given ndims and a vector
Expand Down

0 comments on commit b1c2ca7

Please sign in to comment.