编写一个程序,使用冒泡排序对二维数组中的整数进行升序排序
Write a program that uses Bubble Sort to sort integers in a 2 dimensional array in ascending order
如问题所示,我需要使用冒泡排序对二维数组进行排序。这应该适用于任何 N*M 数组。
我知道我们不应该在没有先尝试任何事情的情况下提出问题。但是我的时间很紧,现在正在学习 C++。我找不到任何包含合适信息的链接来对此进行编码。
如果有人能帮我解决这个问题,那就太好了
您可以将指向二维数组第一行的指针转换为指向 int 的指针,并将该数组作为一维数组进行排序。
给你
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
void bubble_sort( int *a, size_t n )
{
for ( size_t last /* = n */; not ( n < 2 ); n = last )
{
for ( size_t i = last = 1; i < n; i++ )
{
if ( a[i] < a[i-1] )
{
std::swap( a[i], a[i-1] );
last = i;
}
}
}
}
int main()
{
const size_t N = 3;
const size_t M = 4;
int a[N][M];
std::srand( ( unsigned int )std::time( nullptr ) );
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < M; j++ ) a[i][j] = std::rand() % ( M * N );
}
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 << std::endl;
}
std::cout << std::endl;
bubble_sort( reinterpret_cast<int *>( a ), N * 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 << std::endl;
}
return 0;
}
程序输出可能看起来像
7 3 8 7
6 8 5 0
10 9 9 3
0 3 3 5
6 7 7 8
8 9 9 10
另一种方法是将函数bubble_sort
写成模板函数。在这种情况下,它可能如下面的演示程序所示。
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
template <typename T, size_t N, size_t M>
void bubble_sort( T ( &a )[N][M] )
{
for ( size_t n = N * M, last /* = n */; not ( n < 2 ); n = last )
{
for ( size_t i = last = 1; i < n; i++ )
{
if ( a[i / M][i % M] < a[( i - 1 ) / M][( i - 1 ) % M] )
{
std::swap( a[i / M][i % M], a[( i - 1 ) / M][( i - 1 ) % M] );
last = i;
}
}
}
}
int main()
{
const size_t N = 3;
const size_t M = 4;
int a[N][M];
std::srand( ( unsigned int )std::time( nullptr ) );
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < M; j++ ) a[i][j] = std::rand() % ( M * N );
}
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 << std::endl;
}
std::cout << std::endl;
bubble_sort( a );
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 << std::endl;
}
return 0;
}
如问题所示,我需要使用冒泡排序对二维数组进行排序。这应该适用于任何 N*M 数组。
我知道我们不应该在没有先尝试任何事情的情况下提出问题。但是我的时间很紧,现在正在学习 C++。我找不到任何包含合适信息的链接来对此进行编码。
如果有人能帮我解决这个问题,那就太好了
您可以将指向二维数组第一行的指针转换为指向 int 的指针,并将该数组作为一维数组进行排序。
给你
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
void bubble_sort( int *a, size_t n )
{
for ( size_t last /* = n */; not ( n < 2 ); n = last )
{
for ( size_t i = last = 1; i < n; i++ )
{
if ( a[i] < a[i-1] )
{
std::swap( a[i], a[i-1] );
last = i;
}
}
}
}
int main()
{
const size_t N = 3;
const size_t M = 4;
int a[N][M];
std::srand( ( unsigned int )std::time( nullptr ) );
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < M; j++ ) a[i][j] = std::rand() % ( M * N );
}
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 << std::endl;
}
std::cout << std::endl;
bubble_sort( reinterpret_cast<int *>( a ), N * 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 << std::endl;
}
return 0;
}
程序输出可能看起来像
7 3 8 7
6 8 5 0
10 9 9 3
0 3 3 5
6 7 7 8
8 9 9 10
另一种方法是将函数bubble_sort
写成模板函数。在这种情况下,它可能如下面的演示程序所示。
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
template <typename T, size_t N, size_t M>
void bubble_sort( T ( &a )[N][M] )
{
for ( size_t n = N * M, last /* = n */; not ( n < 2 ); n = last )
{
for ( size_t i = last = 1; i < n; i++ )
{
if ( a[i / M][i % M] < a[( i - 1 ) / M][( i - 1 ) % M] )
{
std::swap( a[i / M][i % M], a[( i - 1 ) / M][( i - 1 ) % M] );
last = i;
}
}
}
}
int main()
{
const size_t N = 3;
const size_t M = 4;
int a[N][M];
std::srand( ( unsigned int )std::time( nullptr ) );
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < M; j++ ) a[i][j] = std::rand() % ( M * N );
}
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 << std::endl;
}
std::cout << std::endl;
bubble_sort( a );
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 << std::endl;
}
return 0;
}