467,879 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,879 developers. It's quick & easy.

Sorting

Hi,

I have an array that I want to sort without losing the index
information.

For eg. sorting X[5] = {34.5, 56.4, 12.9, 43.2, 21.7};

should return {2, 4, 0, 3, 1}

Could you please tell me how to do it quickly.

Many thanks,
Speed.
Feb 23 '08 #1
  • viewed: 1727
Share:
6 Replies
Eric Sosman wrote:
[...]
Solution #2: Leave X[] alone and instead sort an array of
indices to it:

double X[] = { 34.5, 56.4, 12.9, ... };
double I[] = { 0, 1, 2, ... };
[...]
Oh, rats. `int I[]', of course, or even `size_t I[]'
if you anticipate sorting large arrays. Sorry about that.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Feb 23 '08 #2
Speed wrote:
) I have an array that I want to sort without losing the index
) information.
)
) For eg. sorting X[5] = {34.5, 56.4, 12.9, 43.2, 21.7};
)
) should return {2, 4, 0, 3, 1}
)
) Could you please tell me how to do it quickly.

Start with {0,1,2,3,4}. Sort this, using X[a] <=X[b] as operator.

The qsort() library function is usually your best bet.
NB: The <=operator is not C. I use it as shorthand for:
X[a] < X[b] ? -1 : X[a] X[b]
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Feb 24 '08 #3
On Feb 23, 8:03 pm, Willem <wil...@stack.nlwrote:
Speed wrote:

) I have an array that I want to sort without losing the index
) information.
)
) For eg. sorting X[5] = {34.5, 56.4, 12.9, 43.2, 21.7};
)
) should return {2, 4, 0, 3, 1}
)
) Could you please tell me how to do it quickly.

Start with {0,1,2,3,4}. Sort this, using X[a] <=X[b] as operator.

The qsort() library function is usually your best bet.

NB: The <=operator is not C. I use it as shorthand for:
X[a] < X[b] ? -1 : X[a] X[b]

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Hi,

Many thanks for the replies. I am not quite sure how to use qsort() to
sort the array of indices. Could you please tell me how to call with
indices?

Many thanks,
Speed.
Feb 25 '08 #4
Speed wrote:
) Many thanks for the replies. I am not quite sure how to use qsort() to
) sort the array of indices. Could you please tell me how to call with
) indices?

I don't know the details of what you're trying to achieve, so how about
you post whatever you think looks like a reasonable piece of code that
tries to do what you want, and then some of the people on this newsgroup
will undoubtedly help out with the bits that aren't working for you.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Feb 25 '08 #5
Willem said:
Richard wrote:
Please retain my surname when quoting, Willem - not because I'm precious
about it or anything, but simply to disambiguate me from the Riley troll.
Thanks.
)
) Um, why not just sort pointers?

Good idea, although that doubles the amount of data that qsort
has to shuffle. But maybe we can do without. How about:
Yes, as soon as you said "although that doubles", I thought of a way to do
without. (Yes, same way that you thought of, although it took me a little
longer!)

<snip>
Although that's still a lot of extra overhead just to avoid
a global pointer.
<shrugIt's a point of view. Some people would see the overhead as
insignificant compared to the maintainability issues of file scope
objects.

I do wish qsort allowed a void *extra, though.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 25 '08 #6
On 25 Feb 2008 at 19:23, Richard Heathfield wrote:
Willem said:
>Richard wrote:

Please retain my surname when quoting, Willem - not because I'm precious
about it or anything,
You? Precious about something? Unthinkable.
but simply to disambiguate me from the Riley troll.
Ha! I can't imagine *he'd* be too pleased to be mixed up with *you* ...
Feb 25 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by dont bother | last post: by
7 posts views Thread by Federico G. Babelis | last post: by
19 posts views Thread by Owen T. Soroke | last post: by
4 posts views Thread by Ambica Jain | last post: by
7 posts views Thread by Kamal | last post: by
5 posts views Thread by lemlimlee | last post: by
5 posts views Thread by jrod11 | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.