Question: I am trying to do some DNA analysis in C++.
I have a 2D array of characters containing DNA sequences. `char input[n][m];`

Now based on the DNA pattern, I do some processing on the input array and find a new sequence for this pattern.

Let say the original sequence was `1,2,3,4,5,6`. These are the 6 DNA's (rows of the 2D array)

After processing i get the order as `1,6,2,4,3,5`. Then how do I rearrange the original input array as per this sequence. That is, the first row of input should now have 1st DNA, 2nd row should now have the DNA which was sixth in the original array and so on. 

```
 int main()

{

using namespace std;
char input[t][l] = {    {'A','A','A','T','C','A','G','T','A'},
                        {'A','T','T','A','C','T','G','C','A'},
                        {'A','G','C','T','A','C','T','G','C'},
                        {'A','T','T','A','C','T','G','C','A'},
                        {'A','G','C','T','A','C','T','G','C'}

                 };

// SOME PROCESSING

Here output array has the elements ={1,6,2,4,3,5}

  for (int i = 0; i<t; i++)
  {
  cout << output[i] << " ";
  }  
}

```

Any ideas how to change the addresses in the memory so as re-order the original array?

Answer: 
This would only move around pointers:

```
char input[5][9] = {    {'A','A','A','T','C','A','G','T','A'},
                        {'A','T','T','A','C','T','G','C','A'},
                        {'A','G','C','T','A','C','T','G','C'},
                        {'A','T','T','A','C','T','G','C','A'},
                        {'A','G','C','T','A','C','T','G','C'}
                 };

char* output[5];
output[0] = input[3];
output[1] = input[2];
output[2] = input[4];

char test = output[1][3];

```
If you don't need to do it in place, just create new destination matrix, copy row 1 to 1, row 6 to row 2, and so on... 
Just use output[i] as index for row of original matrix.

With some more bookkeeping, it can certainly also be done in place.
Ok, if you need in-place, I'd do something like this:
You have your pair list of [source row][destination row], like:

[1]<-[1],
[2]<-[6],
[3]<-[2],
...

Create map describing this copy operations. Then just start from 1.

[1]<-[1] is easy. (You can remove it from map)

[2]<-[6] overwrites your original row 2, which you need in next step. So you exchange rows 2 and 6. Now row 6 is in right place, but you need to change mapping. You need to change mapping [3]<-[2] to [3]<-[6] using something like like map[2]=6.

And so on, until done.