sorting dynamic 2D array

I want to sort a 2D DYNAMIC array horizontaly & verticaly. I want to do it 1 row at a time, such as
123
312
231 horizontally to:
123
123
123
and the same array
123
312
231 vertically to:
111
222
333
Could someone explain me have to do it, because i need to sort every row and compare if they are the same as different dynamic 1D array[n] //n=2 in the example above.
is this the same as just sorting a 1-d array normally? If so, you can copy to a 1-d, sort it, and copy back (bad approach, but it works), or you can just use a 1-d as a 2-d via indexing or pointer tricks. That is an easy/efficient way, with the bonus that you can compare to your 1-d either directly (vector) or with memcmp (array/pointer).

another way is to sort it 1 row at a time as said and do some sort of transpose on it, does that work?


Last edited on
My question is how to sort it 1 row at a time. Would it be something like-
1
2
3
4
5
6
int** A;
cin>>n;
A= new int [n];
for (int i=0:i<n;i++
for (int j=0;j<n;j++)
sort(A[i];A[i][n]

??
@eci0x,

WHY do you want to sort a 2-d array? (Even supposing such a thing was meaningful).

If you are trying to decide whether it is a latin square (as in your other triplicate posts) then you simply need to determine whether each number 1,2,...,n appears precisely once in each row and in each column. You don't need to sort anything to do that.
Last edited on
std::sort will do a full row at a time.
so you need a loop over all the rows.

if the '2d array' is exactly that (an array) you can probably call std sort on mxn and do the entire thing at once as if 1d. This will NOT work on ** dynamic structures. (this part is still trying to answer your original question, which is still a little unclear).

Last edited on
Topic archived. No new replies allowed.