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 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: me |
last post by:
good day,
i found this message:
and i want to know more about it too.
==========================================
Hey all,
example:...
|
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...
|
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;
}
|
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...
|
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?
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
| |