Sort matrix

Good morning, I trying to sort matrix but using for loops but I can't find errors. Thanks for tips.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  #include <iostream>
#include <cstdlib>
#include <ctime>
 
 
using namespace std;
void sort_matrix(int matrix[4][4])         
    int pmin=matrix[0][0];
    int t;
  for(int i=0; i<3; i++)
  {
     for(int j=0; j<3; j++)
   {
     for(int k=i+1; k<4; k++)
    {
      for(int l=j+1; l<4; l++)
      {
          if(matrix[k][l] < pmin)
          {
 
          pmin = matrix[k][l];
 
          t=pmin;
          pmin=matrix[i][j];
          matrix[i][j]=t;
 
          }
      }
 
    }
 
   }
 
  }
}
int main()
{
srand(time(NULL));
int matrix[4][4];
int i,j;
for( i=0; i<4; i++)               
 {
  for( j=0; j<4; j++)
  {
   matrix[i][j]=(rand()%21-10);
    cout<<matrix[i][j]<<" ";
  }
 }
sort_matrix(matrix);
cout<<endl;
for(i=0; i<4; i++)               
 {
  for(j=0; j<4; j++)
  {
    cout<<matrix[i][j]<<" ";
  }
 }
 
    return 0;
}
Last edited on
How do you want it to be sorted?
1 2 3
4 5 6
7 8 9

7 8 9
4 5 6
1 2 3

1 4 7
2 5 8
3 6 9

1 4 8
6 2 5
9 7 3
First case
1 2 3
4 5 6
7 8 9
The first problem is that you're printing the matrix on one line. Add cout << '\n'; after lines 48 & 57.

It looks like you're doing selection sort, whereby you find the smallest element and put it in the first position. Then find the next smallest and put in the second position, etc.

In sort _matrix. you want i & j to traverse the entire matrix, so lines 11 and 13 should be:
1
2
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {


Lines 19-26 should compare matrix[i][j] with matrix[k][l] and swap them if necessary. Get rid of pmin and do it directly:
1
2
3
4
5
6
                    if (matrix[k][l] < matrix[i][j]) {
                        // swap them
                        t = matrix[i][j];
                        matrix[i][j] = matrix[k][l];
                        matrix[k][l] = t;
                    }

Also, consider using std::swap() instead of swapping by hand.

Lastly comes the hardest part, how do you make k and l cover the items that come "after" i and j? k needs to start at i to handle other items on the same row, but j is tricky: is j+1 for the first row and 0 for the others. I solved this with a new variable called startl:
1
2
3
4
5
6
7
8
9
10
11
12
            int startl = j+1;
            for (int k = i; k < 4; k++) {
                for (int l = startl; l < 4; l++) {
                    if (matrix[k][l] < matrix[i][j]) {
                        // swap them
                        t = matrix[i][j];
                        matrix[i][j] = matrix[k][l];
                        matrix[k][l] = t;
                    }
                }
                startl = 0;
            }

Finally, I'll say that another way to do this is to treat the entire matrix as a 1D array of 16 items. I think the standard guarantees that they're stored this way.
Yes, array is a continuous block of memory:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>

constexpr int N {4};

void print( int matrix[N][N] )
{
  for ( int row=0; row < N; ++row ) {
    for ( int col=0; col < N; ++col ) {
      std::cout << ' ' << matrix[row][col];
    }
    std::cout << '\n';
  }
}

int main() {
  srand(time(NULL));
  int matrix[N][N];
  for ( int i=0; i<N; i++) {
    for ( int j=0; j<N; j++) {
      matrix[i][j] = (rand()%21-10);
    }
  }

  print( matrix );
  std::cout << '\n';

  int* raw = matrix[0];
  std::sort( raw, raw+N*N );

  print( matrix );
  std::cout << '\n';
}
Thank you very much for help.
Topic archived. No new replies allowed.