0

I need an argsort in my c++ code. I see some people write their own argsort in C++ like this one https://medium.com/@sddkal/my-take-on-c-argsort-function-bfb59845d30c. But what would be a built-in one or one from a well-known library? Implementations from such libraries should have better performance presumably (like std::vector should have better performance than a hand-crafted one.)

Argsort is to returns a list of index positions as if our target list is sorted. See https://numpy.org/doc/stable/reference/generated/numpy.argsort.html for a description in python's implementation in Numpy for example.

zell
  • 9,830
  • 10
  • 62
  • 115
  • 5
    Well I was going to suggest using the standard sort with a comparator that looks up the values to compare from another vector - but that's exactly what your article does. What's wrong with that? – Rup Oct 26 '20 at 23:49
  • @Rup Implementations from well-known should have better performance presumably. – zell Oct 26 '20 at 23:53
  • 3
    Better performance than what? According to what metrics? Did you measure it? Did you find a bottleneck? Did you analyse the algorithmic complexity of the approach and find it lacking? What? – Asteroids With Wings Oct 26 '20 at 23:55
  • It is like std::vector should have better performance than a hand-crafted one. @AsteroidsWithWings? – zell Oct 26 '20 at 23:56
  • 1
    The top-scoring answer in the duplicate post should be perfectly adequate. This is (almost) as performant as you'll get in C++, or anything for that matter. The only tiny inefficiency is that the vector resize will unnecessarily initialize all that memory before then writing it again with `std::iota`. But you're unlikely to notice any appreciable difference. This is a good demonstration of having a flexible set of tools and algorithms to perform specific tasks such as yours, without requiring such specific implementations cluttering the standard library. – paddy Oct 27 '20 at 00:00
  • http://coliru.stacked-crooked.com/a/e09ec88779a85170 ? – Mooing Duck Oct 27 '20 at 00:03
  • That makes sense, @paddy – zell Oct 27 '20 at 00:04
  • @AsteroidsWithWings That is what Strousttrup said in his book. But of course he may be wrong. – zell Oct 27 '20 at 00:07
  • questions must be self-contained. Besides medium.com is notorious for limiting the number of views each month so lots of people won't be able to read the article to understand what argsort is – phuclv Oct 27 '20 at 02:02
  • @zell I doubt he said that. If he did, yes, he was wrong. – Asteroids With Wings Oct 27 '20 at 11:40

0 Answers0