Skip to content

Implementing an efficient Argsort #1145

Closed
@PABannier

Description

@PABannier

I'm currently working on a project where I need to have a argsort function (in descending order).

// kkt is an instance of Array1<T>

let mut kkt_with_indices: Vec<(usize, T)> = kkt.iter().copied().enumerate().collect();

kkt_with_indices.sort_unstable_by(|(_, p), (_, q)| {
    // Swapped order for sorting in descending order.
    q.partial_cmp(p).expect("kkt must not be NaN.")
});

let ws: Vec<usize> = kkt_with_indices
    .iter()
    .map(|&(ind, _)| ind)
    .take(ws_size)
    .collect();

My implementation works but I think it could be further optimized resorting only to Array, and not passing by Vec, which creates extra memory allocation. In my case this piece of code is very often (possibly 100'000's times up to a million times), so coming up with an efficient argsort function would be amazing.

I've seen an open topic on sorting (#195), but did not find an implementation for argsort. I'd like to implement it as a first contribution to the library, but I need some guidance. Would anybody be willing to help by offering some guidance?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions