sort multiple reference
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
May 2011
Posts
1

## sort multiple reference

Hi,

I have to sort some matrix by different option. It is not about the method really but something with uncommon options. I explain :
I have N matrix of the same size. I want to sort the i-th column of the k-th matrix and duplicate the rearrangement to each other columns and matrix.
Example :

00 20 05 10 07 06 03 04 10 12
03 40 08 02 23 | 50 17 01 10 15
06 30 14 19 33 | 00 18 02 21 34
39 15 00 11 08 16 07 04 22 40

should be sort on reference of the 0th matrix with the 3th column which give :

39 15 00 11 08 16 07 04 22 40
00 20 05 10 07 | 06 03 04 10 12
03 40 08 02 23 | 50 17 01 10 15
06 30 14 19 33 00 18 02 21 34

What type of algorithm already exist which can do that? And How?

2. ## Re: sort multiple reference

Basically you are rearranging each of the matrices rows according to the sorted ordering of a given column in a given matrix.

You just need to figure out what is the new row index of each of the rows.
If you alter you data structure a bit, it should be easy.
Keep on each element 2 values:
a. The actual value of the element
b. The index of the original row of the element.

Before beginning the sort process - initialize the row indices(b) of all elements in the given column.
After the sort (by element value(a) ) is complete, just linearly go over the column elements and the (b) index of the element on row i will tell you which row should be moved to row i.

PS. If you do not wish to alter the data structure, just fill an array of size N with the indices 0 to N-1 and duplicate each sort operation you do on the matrix onto it (equivalent to adding another column of sorted numbers to the matrix).

Kind regards,
Zachi

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•