#include <stdio.h> #include <conio.h> int main() { int i, j; int m, n; int x, y; printf("\nEnter number of rows matrix A \n");//Введите количество строк матрицы А column scanf("%d", &m); printf("\nEnter number of columns matrix A \n");//Введите количество столбцов матрицы А scanf("%d", &n); float A[m][n]; printf("\nEnter matrix A %dx %d elements \n",m,n);//Bведите матрицу A for (i=0;i<m; i++) { printf("\nElements of %d row: \n",i+1);//Элементы %d-й строки for (j=0; j<n; j++) { printf("Elem A [%d][%d]", i+1, j+1); scanf("%f", &A[i][j]); } } printf("\nMatrix A: \n");//Матрица А for(i=0; i<m; i++) { for(j=0; j<n; j++) { printf("%f\t",A[i][j]); } printf("\n"); } for(y=0; y<m; y++) { for (i=0; i<(m-1); i++) { if(A[i][0]>A[i+1][0]) { for(j=0; j<n; j++) { x=A[i][j]; A[i][j]=A[i+1][j]; A[i+1][j]=x; } } } } printf("\n"); printf("\nSorted matrix A: \n");//Сортированная матрица for(i=0; i<m; i++) { for(j=0; j<n; j++) { printf("%f\t",A[i][j]); } printf("\n"); } printf("\n"); } 

The task is to order the matrix in ascending order of values ​​in rows by columns. If there are matching values ​​in the column, then move to the next column and order by it, etc. For example, if the original

 1 7 3 4 1 2 4 3 1 2 1 4 

it should work

 1 2 1 4 1 2 4 3 1 7 3 4 

I rummaged through many similar topics, there are similar bubble sortings everywhere, like mine.

But the problem is that if the matrix, for example, 5 x 5 and it has all the numbers in the first, second and third columns, then it does not sort by the fourth and fifth. I checked both on the matrix easier and less, and it turned out that it sorts only by the first column. How can you change this code so that the sorting of the rows is checked for matching and takes place across all columns

  • it is necessary to sort the matrix by the first column, then by the second sort the subsets in which 1 column is the same, then by the third column subsets in which 2 are the same, and so on until the end. - Andrey Golikov
  • after sorting the next column, you will have the same numbers in a row. Take the first number in the column and run down until you meet another number, so determine the boundaries of the subsets and sort them, repeat the procedure for the 2 columns and so on until the last column ... - Andrey Golikov
  • @AndreyGolikov I do not think that it is necessary to use block sorts a la scoop where there is no great benefit from them. Especially a newbie who can make a lot of mistakes there. - pavel

1 answer 1

In fact, you need to write a comparator correctly. Then you can use the standard function (the label c and c++ is exactly the case).

 #include <stdio.h> #include <stdlib.h> const int N = 3; const int M = 4; int a[N][M] = { {1,7,3,4},{1,2,4,3}, {1,2,1,4} }; int cmp(const void *a, const void *b){ const int *aa = (const int *)a; const int *bb = (const int *)b; for (int i=0;i<M;i++) if (aa[i] != bb[i]) return aa[i] - bb[i]; return 0; } int main() { qsort(a, N, sizeof(int)*M,cmp); for (int i=0;i<N;i++){ for (int j=0;j<M;j++) printf("%d ",a[i][j]); printf("\n"); } return 0; } 

You compared only the first element if(A[i][0]>A[i+1][0]) , in fact, as it was written, it worked like that.

If there are no negative numbers ( unsigned data type), then you can write like this:

 int cmp(const void *a, const void *b){ return memcmp(a,b,sizeof(int)*M); }