menu

arrow_back What is the matrix sorting error?

by
1 vote
The task is to create an algorithm that will sort a matrix from a two-dimensional array as follows: first there are negative numbers, then zeros, and then positives. It seems to be done, but I do not understand what my mistake is, if anything, N - the number of rows and M - columns
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (A[i][j] > 0) {
for (int k = i + 1; k < N; k++) {
for (int l = j + 1; l < M; l++) {
if (A[k][l] < 0) {
swap(A[k][l], A[i][j]);
}
}
}
}
}
}

for (int i = M - 1; i >= 0; i--) {
for (int j = N - 1; j >= 0; j--) {
if (A[i][j] < 0) {
for (int k = i; k >= 0; k--) {
for (int l = j; l >= 0; l--) {
if (A[k][l] > 0) {
swap(A[k][l], A[i][j]);
}
}
}
}
}
}
5f9b22e3e0243031163634.png

1 Answer

by
0 votes
M and N switched places
for (int k = i + 1; k < N; k++) {
for (int l = j + 1; j < M; l++) {