我想以非降序对输入数组进行行和列排序。我想当我试图转置我的数组时出现问题。我应该更改我的代码吗?
假设我们输入了这样的内容:
10 10 10 10
2 3 1 4
5 6 8 7
我想得到的输出:
1 2 3 4
5 6 7 8
10 10 10 10
输出即时消息(错误):
1 2 3 0
5 6 7 0
10 10 10 0
int** sortRowCol(int **&A, int n, int m) {
sortRow(A, n);
int **T = transpose(A, n, m);
sortRow(T, n);
A = transpose(T, n, m);
return A;
}
int **transpose(int **A, int n, int m) {
int **T = new int *[m];
for (size_t i = 0; i < m; ++i) {
T[i] = new int[n];
for (int j = 0; j < n; ++j)
T[i][j] = A[j][i];
}
return T;
}
void sortRow(int **&A, int n) {
for (size_t i = 0; i < n; ++i)
std::sort(A[i], A[i] + n);
}
int main() {
int n, m;
std::cout << "Enter number of rows and columns: \n";
std::cin >> n >> m;
int **A = new int *[n];
for (size_t i = 0; i < n; ++i)
A[i] = new int[m];
std::cout << "Fill array: \n";
for (size_t i = 0; i < n; ++i)
for (size_t j = 0; j < m; ++j)
std::cin >> A[i][j];
A = sortRowCol(A,n,m);
printArray(A,n,m);
deleteArray(A, n);
return 0;
}
您的方法太复杂,而且存在内存泄漏。
和此功能
void sortRow(int **&A, int n) {
for (size_t i = 0; i < n; ++i)
std::sort(A[i], A[i] + n);
}
仅在平方矩阵的n
等于m
时可以工作。
首先对每个“行”进行排序,然后对指向“行”的指针数组进行排序要简单得多。
您在这里。
#include <iostream>
#include <iomanip>
#include <algorithm>
int main()
{
size_t n = 3;
size_t m = 4;
int **a = new int *[n];
for ( size_t i = 0; i < n; i++ )
{
switch ( i )
{
case 0:
a[i] = new int[m] { 10, 10, 10, 10 };
break;
case 1:
a[i] = new int[m] { 2, 3, 1, 4 };
break;
case 2:
a[i] = new int[m] { 5, 6, 8, 7 };
}
}
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < m; j++ )
{
std::cout << std::setw( 2 ) << a[i][j] << ' ';
}
std::cout << '\n';
}
putchar( '\n' );
for ( size_t i = 0; i < n; i++ )
{
std::sort( a[i], a[i] + m );
}
std::sort( a, a + n,
[m]( const int *p1, const int *p2 )
{
return std::lexicographical_compare( p1, p1 + m, p2, p2 + m );
} );
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < m; j++ )
{
std::cout << std::setw( 2 ) << a[i][j] << ' ';
}
std::cout << '\n';
}
putchar( '\n' );
for ( size_t i = 0; i < n; i++ )
{
delete [] a[i];
}
delete [] a;
return 0;
}
程序输出为
10 10 10 10
2 3 1 4
5 6 8 7
1 2 3 4
5 6 7 8
10 10 10 10
如果您还希望在对每一行进行排序之后对一列中的元素进行排序,则程序可以按照以下方式查找。
#include <iostream>
#include <iomanip>
#include <algorithm>
void sort_row( int a[], size_t n )
{
std::sort( a, a + n );
}
int main()
{
size_t n = 3;
size_t m = 4;
int **a = new int *[n];
for ( size_t i = 0; i < n; i++ )
{
switch ( i )
{
case 0:
a[i] = new int[m] { 20, 20, 10, 30 };
break;
case 1:
a[i] = new int[m] { 10, 40, 10, 20 };
break;
case 2:
a[i] = new int[m] { 20, 20, 10, 10 };
}
}
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < m; j++ )
{
std::cout << std::setw( 2 ) << a[i][j] << ' ';
}
std::cout << '\n';
}
putchar( '\n' );
for ( size_t i = 0; i < n; i++ )
{
sort_row( a[i], m );
}
int *tmp = new int[n];
for ( size_t i = 0; i < m; i++ )
{
for ( size_t j = 0; j < n; j++ )
{
tmp[j] = a[j][i];
}
sort_row( tmp, n );
for ( size_t j = 0; j < n; j++ )
{
a[j][i] = tmp[j];
}
}
delete [] tmp;
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < m; j++ )
{
std::cout << std::setw( 2 ) << a[i][j] << ' ';
}
std::cout << '\n';
}
putchar( '\n' );
for ( size_t i = 0; i < n; i++ )
{
delete [] a[i];
}
delete [] a;
return 0;
}
程序输出为
20 20 10 30
10 40 10 20
20 20 10 10
10 10 20 20
10 10 20 30
10 20 20 40