The basic algorithm of C language 42-matrix transpose and sort by row by column

Source: Internet
Author: User

Matrix transpose by row Sort by column
/*
==================================================================
Title: Enter the M*n matrix and sort the output in ascending rows.
Input:
4 3 5) 6 2
9 8 1) 2 8
7 1 2) 3 8
Output:
2 3 4) 5 6
1 2 8) 8 9
1 2 3) 7 8
==================================================================
*/
#include <stdio.h>
#define M 3
#define N 5
Main ()
{
int i,j,k,a[m][n],b[n][m],c[m][n],d[m][n],t;
Enter the original matrix A
printf ("Input%d*%d matrix: \ n", m,n);
for (i=0;i<m;i++)
for (j=0;j<n;j++)
scanf ("%d", &a[i][j]);
Output Original matrix A
printf ("\ n original sequence output: \ n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
printf ("%3d", A[i][j]);
printf ("\ n");
}
Copy the original matrix A to C
for (i=0;i<m;i++)
for (j=0;j<n;j++)
C[I][J]=A[I][J];
The original matrix is sorted by row
printf ("Ascending by line: \ n");
i=0;
while (I&LT;M)
{
for (j=0;j<n;j++)
for (k=j+1;k<n;k++)
if (A[i][j]>a[i][k])
{t=a[i][j];a[i][j]=a[i][k];a[i][k]=t;}
i++;
}
Result of output sorted by row
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
printf ("%3d", A[i][j]);
printf ("\ n");
}


The original matrix transpose gets B
for (i=0;i<m;i++)
for (j=0;j<n;j++)
B[J][I]=C[I][J];
i=0;
Output Transpose Matrix B
printf ("Output transpose matrix: \ n");
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
printf ("%3d", B[i][j]);
printf ("\ n");
}
After transpose (i.e. B) sort by row
i=0;
while (I<n)
{
for (j=0;j<m;j++)
for (k=j+1;k<m;k++)
if (B[i][j]>b[i][k])
{t=b[i][j];b[i][j]=b[i][k];b[i][k]=t;}
i++;

}
Then transpose B, that is, to return to the prototype after sorting
printf ("Sort by column: \ n");
for (i=0;i<n;i++)
for (j=0;j<m;j++)
D[J][I]=B[I][J];
Transpose matrix D of output B, sorted by column for the original matrix
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
printf ("%3d", D[i][j]);
printf ("\ n");
}


}




/*
==================================================================
Review: Changing the values of M (rows) and N (columns) allows for the transpose of any two-dimensional matrix, sorted by row by column.
Divided into three sections:
1. Input
2, the original sequence output
3, ascending by row
The key is the third step, using the while implementation of J prior to I, after the J cycle is completed i++, until the i>m;
Sort By column can be considered to be the transpose matrix sorted by row, with two times transpose complete!
==================================================================
*/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The basic algorithm of C language 42-matrix transpose and sort by row by column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.