473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
6 2351
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.