446,159 Members | 979 Online Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

# sort indices

 P: n/a I want to get the sort indices of an valarray ie. for type int and array valarray a( 0, 4); a = 7, a = 2, a = 9, a = 4, I want to get as output a valarray res with: res == 1 // index of '2' first el res == 3 // 4 2nd el res == 0 // 7 3rd el res == 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 Replies

 P: n/a Marc Schellens wrote: I want to get the sort indices of an valarray ie. for type int and array valarray a( 0, 4); a = 7, a = 2, a = 9, a = 4, I want to get as output a valarray res with: res == 1 // index of '2' first el res == 3 // 4 2nd el res == 0 // 7 3rd el res == 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; res = 1; res = 2; res = 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

 P: n/a Karl Heinz Buchegger wrote: Marc Schellens wrote:I want to get the sort indices of an valarrayie.for type int and arrayvalarray a( 0, 4);a = 7,a = 2,a = 9,a = 4,I want to get as output a valarray reswith:res == 1 // index of '2' first elres == 3 // 4 2nd elres == 0 // 7 3rd elres == 2 // 9 last elAll the sort* algorithms sort the array itsef, howto 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; res = 1; res = 2; res = 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

 P: n/a On Wed, 11 Aug 2004 18:07:07 +0900, Marc Schellens wrote: I want to get the sort indices of an valarrayie.for type int and arrayvalarray a( 0, 4);a = 7,a = 2,a = 9,a = 4, I want to get as output a valarray reswith:res == 1 // index of '2' first elres == 3 // 4 2nd elres == 0 // 7 3rd elres == 2 // 9 last elAll the sort* algorithms sort the array itsef, howto get those indices most easily?Speed is an issue. An alternative to Karl's suggestion is to use a valarray (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; Tom Jul 22 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 