# Avoid allocation of small arrays in median, extrema, etc

**URL:** <https://discourse.julialang.org/t/avoid-allocation-of-small-arrays-in-median-extrema-etc/58075>\
**Category:** Performance\
**Tags:** memory-allocation\
**Created:** [March 27, 2021, 12:22pm UTC](https://discourse.julialang.org/t/avoid-allocation-of-small-arrays-in-median-extrema-etc/58075 "2021-03-27T12:22:46Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [March 28, 2021, 6:34pm UTC](https://discourse.julialang.org/t/avoid-allocation-of-small-arrays-in-median-extrema-etc/58075/23 "2021-03-28T18:34:41Z")

</div>

The direct link is

> **[An Efficient Algorithm for the Approximate Median Selection Problem](https://link.springer.com/chapter/10.1007/3-540-46521-9_19)**
>
> We present an efficient algorithm for the approximate median selection problem. The algorithm works in-place; it is fast and easy to implement. For a large array it returns, with high probability, a very close estimate of the true median. The running...

(Referencing via Resg… can trigger email spamming, see the criticism section on Wikipedia)

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [March 28, 2021, 6:37pm UTC](https://discourse.julialang.org/t/avoid-allocation-of-small-arrays-in-median-extrema-etc/58075/24 "2021-03-28T18:37:20Z")

</div>

Thank you, I’ve changed the link, was copying from the phone, sorry.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [March 28, 2021, 8:20pm UTC](https://discourse.julialang.org/t/avoid-allocation-of-small-arrays-in-median-extrema-etc/58075/25 "2021-03-28T20:20:50Z")

</div>

Thanks, these look neat.

BTW, a slightly less awful algorithm than `unrollmedian` above is to use `TupleTools.sort(x)`.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [March 28, 2021, 9:45pm UTC](https://discourse.julialang.org/t/avoid-allocation-of-small-arrays-in-median-extrema-etc/58075/26 "2021-03-28T21:45:48Z")

</div>

May be of interest:  
Sorting via branch’s.

[https://github.com/JeffreySarnoff/SortingNetworks.jl](https://github.com/JeffreySarnoff/SortingNetworks.jl)

[Previous page](https://discourse.julialang.org/t/avoid-allocation-of-small-arrays-in-median-extrema-etc/58075.md?page=1)
