# Find n smallest values in an n dims array

**URL:** <https://discourse.julialang.org/t/find-n-smallest-values-in-an-n-dims-array/81092>\
**Category:** New to Julia\
**Tags:** question, sort, sortperm, arrays, cartesianindices\
**Created:** [May 15, 2022, 12:07pm UTC](https://discourse.julialang.org/t/find-n-smallest-values-in-an-n-dims-array/81092 "2022-05-15T12:07:22Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 16, 2022, 3:53pm UTC](https://discourse.julialang.org/t/find-n-smallest-values-in-an-n-dims-array/81092/21 "2022-05-16T15:53:18Z")

</div>

> [@Seif\_Shebl](#):
>
> You may check the source code for `findmin` and `argmin` and possibly submit a pull request. However, I discovered that many Julia algorithms are optimized for huge inputs; for instance, at around 10^8, they are almost unbeatable.

```julia
julia> A = rand(0x0:0x8f, 1_000_000);

julia> @which findmin(A)
findmin(A::AbstractArray; dims) in Base at reducedim.jl:1005

julia> @less findmin(A)

```

```julia
findmin(A::AbstractArray; dims=:) = _findmin(A, dims)

function _findmin(A, region)
    ri = reduced_indices0(A, region)
    if isempty(A)
        if prod(map(length, reduced_indices(A, region))) != 0
            throw(ArgumentError("collection slices must be non-empty"))
        end
        (similar(A, ri), zeros(eltype(keys(A)), ri))
    else
        findminmax!(isgreater, fill!(similar(A, ri), first(A)),
                    zeros(eltype(keys(A)), ri), A)
    end
end

```

The code:

> <https://github.com/JuliaLang/julia/blob/v1.7.2/base/reducedim.jl#L1005>

---

<div class="post-metadata">

**Author:** ![Xiao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiao/32/29220_2.png) [@Xiao](https://discourse.julialang.org/u/Xiao)\
**Post date:** [May 26, 2023, 4:01am UTC](https://discourse.julialang.org/t/find-n-smallest-values-in-an-n-dims-array/81092/22 "2023-05-26T04:01:35Z")

</div>

Find them as below, for example the first 3 values.

```julia
julia> a = [100 1 10; 20 200 2]
2×3 Matrix{Int64}:
 100 1 10
  20 200 2

julia> first(a |> vec |> unique |> sort, 3)
3-element Vector{Int64}:
  1
  2
 10

```

---

<div class="post-metadata">

**Author:** ![florian.dubois](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/florian.dubois/32/220935_2.png) [@florian.dubois](https://discourse.julialang.org/u/florian.dubois)\
**Post date:** [February 13, 2026, 2:07pm UTC](https://discourse.julialang.org/t/find-n-smallest-values-in-an-n-dims-array/81092/23 "2026-02-13T14:07:33Z")

</div>

Hi !  
I’ve used your code on my work, so I am submitting the slightly improved version I’m using:

```julia-auto
function argsmallest(A::AbstractArray{T,N}, n::Integer) where {T,N}
    # should someone ask more elements than array size, just sort array
    if n>= length(vec(A))
        ind=collect(1:length(vec(A)))
        ind=sortperm(A[ind])
        return CartesianIndices(A)[ind]
    end

    # otherwise 
    ind=collect(1:n)
    mymax,getout=findmax(@view A[1:n])
    for j=n+1:length(vec(A))
        if A[j]<mymax
            ind[getout]=j
            mymax,getout=findmax(@view A[ind])
        end
    end
    
    return ind
end

```

I just add a tracker for `mymax` index and some `@view` to avoid memory allocation on access

[Previous page](https://discourse.julialang.org/t/find-n-smallest-values-in-an-n-dims-array/81092.md?page=1)
