473,385 Members | 1,312 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,385 software developers and data experts.

sort indices

I want to get the sort indices of an valarray<type>
ie.
for type int and array

valarray<int> a( 0, 4);
a[0] = 7,
a[1] = 2,
a[2] = 9,
a[3] = 4,
I want to get as output a valarray<long> res
with:
res[0] == 1 // index of '2' first el
res[1] == 3 // 4 2nd el
res[2] == 0 // 7 3rd el
res[3] == 2 // 9 last el

All the sort* algorithms sort the array itsef, how
to get those indices most easily?
Speed is an issue.

Thanks,
Marc

Jul 22 '05 #1
3 2775
Marc Schellens wrote:

I want to get the sort indices of an valarray<type>
ie.
for type int and array

valarray<int> a( 0, 4);
a[0] = 7,
a[1] = 2,
a[2] = 9,
a[3] = 4,

I want to get as output a valarray<long> res
with:
res[0] == 1 // index of '2' first el
res[1] == 3 // 4 2nd el
res[2] == 0 // 7 3rd el
res[3] == 2 // 9 last el

All the sort* algorithms sort the array itsef, how
to get those indices most easily?


By modifying the sort criterium to take this
indirection into account.

You start with an res array of
res[0] = 0;
res[1] = 1;
res[2] = 2;
res[3] = 3;

and sort that. But when the sort algorithm compares comes
to the part when the comparison of array entries is done,
you don't compare res[i] with res[j], but instead
you compare a[res[i]] with a[res[j]]

That's all. It's really very simple.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Karl Heinz Buchegger wrote:
Marc Schellens wrote:
I want to get the sort indices of an valarray<type>
ie.
for type int and array

valarray<int> a( 0, 4);
a[0] = 7,
a[1] = 2,
a[2] = 9,
a[3] = 4,

I want to get as output a valarray<long> res
with:
res[0] == 1 // index of '2' first el
res[1] == 3 // 4 2nd el
res[2] == 0 // 7 3rd el
res[3] == 2 // 9 last el

All the sort* algorithms sort the array itsef, how
to get those indices most easily?

By modifying the sort criterium to take this
indirection into account.

You start with an res array of
res[0] = 0;
res[1] = 1;
res[2] = 2;
res[3] = 3;

and sort that. But when the sort algorithm compares comes
to the part when the comparison of array entries is done,
you don't compare res[i] with res[j], but instead
you compare a[res[i]] with a[res[j]]

That's all. It's really very simple.


Well, ahem...
thanks,
Marc
Jul 22 '05 #3
On Wed, 11 Aug 2004 18:07:07 +0900, Marc Schellens
<m_*********@hotmail.com> wrote:
I want to get the sort indices of an valarray<type>
ie.
for type int and array

valarray<int> a( 0, 4);
a[0] = 7,
a[1] = 2,
a[2] = 9,
a[3] = 4,
I want to get as output a valarray<long> res
with:
res[0] == 1 // index of '2' first el
res[1] == 3 // 4 2nd el
res[2] == 0 // 7 3rd el
res[3] == 2 // 9 last el

All the sort* algorithms sort the array itsef, how
to get those indices most easily?
Speed is an issue.


An alternative to Karl's suggestion is to use a valarray<int*> (where
each element is the address of the equivalent element in the original
valarray), sort that using a comparator that dereferences the values
before comparing them, and then copy the resulting array into your
result, with the index of an element being
res[i] = sorted[i] - &a[0];

Tom
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: me | last post by:
good day, i found this message: and i want to know more about it too. ========================================== Hey all, example:...
8
by: b83503104 | last post by:
In matlab, the sort function returns two things: =sort() a = 5 7 8 b = 1 3 2 where a is the sorted result, and b is the corresponding index. Is there C++ code available to...
8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
3
by: gambler | last post by:
let's say you have: var games = new Array(); games = new GAME(gameNum, rotNum1, rotNum2, ... ); ( so a sparsley populate array which enables me to locate a game usin the game number...
15
by: KraftDiner | last post by:
I have two lists. I want to sort by a value in the first list and have the second list sorted as well... Any suggestions on how I should/could do this?
5
by: Jan Smith | last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a clear answer to my question. Maybe it's not in Array.Sort. Here's the question: I initialize an array X with the...
23
by: Brian Blais | last post by:
Hello, I have two lists, one with strings (filenames, actually), and one with a real-number rank, like: A= B= I'd like to sort list A using the values from B, so the result would be in...
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
0
by: =?iso-8859-1?Q?=22Orlando_D=F6hring=22?= | last post by:
Dear community, I want to use the sort function to sort a (nested) list. General information can be found below. http://www.python.org/doc/2.4.2/lib/typesseq-mutable.html...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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...
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...

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.