Sorting a vector of (double precision) reals and obtain their order

Posted by Philipp on Stack Overflow See other posts from Stack Overflow or by Philipp
Published on 2010-12-23T23:30:09Z Indexed on 2010/12/23 23:54 UTC
Read the original article Hit count: 289

Filed under:
|
|
|
|

Hello everyone,

in C++ would like to sort a lengthy (2^20) vector of reals, obviously sort() does the trick. Having used R before I was used to the nice order() function which yields the permutation that leads to the sorted vector. Probably someone has done this in C++, maybe it's just my weak google-Fu that prevents me from finding it. And yeah, obivously my C++ newbness could stop me from spotting something straightforward.

Example:

x = {24, 55, 22, 1}

then the permutation

perm = {3, 2, 0, 1}

maps the original x to the sorted x in ascending order.

I can probably implement some bubble sort which does not only sort x but performs the same transpositions on the vector {0,1,2,...} and outputs both, but I believe someone must have thought about it and especially have done it efficiently.

Thank you very much, Philipp

© Stack Overflow or respective owner

Related posts about c++

Related posts about sorting