C++ 编写一个程序,使用冒泡排序按升序对二维数组中的整数进行排序

C++ 编写一个程序,使用冒泡排序按升序对二维数组中的整数进行排序,c++,arrays,sorting,bubble-sort,C++,Arrays,Sorting,Bubble Sort,如问题所示,我需要使用冒泡排序对二维数组进行排序。这应该适用于任何N*M阵列 我知道我们不应该在不尝试任何事情的情况下提问。但是我的日程安排很紧,现在我正在学习C++。我找不到任何包含适当信息的链接来编写此代码 如果有人能帮我解决这个问题,那就太好了。您可以将指向二维数组第一行的指针转换为指向int的指针,并将数组排序为一维数组 给你 #include <iostream> #include <iomanip> #include <cstdlib> #incl

如问题所示,我需要使用冒泡排序对二维数组进行排序。这应该适用于任何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;
}
另一种方法是将函数
bubble\u 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;
}
#包括
#包括
#包括
#包括
模板
无效气泡排序(T&a)[N][M])
{
对于(尺寸n=n*M,最后一个/*=n*/;非(n<2);n=最后一个)
{
对于(大小i=last=1;istd::难道我们不是一个代码编写服务。你尝试过什么?抱歉,即使你的日程安排很紧,你也不会在这方面得到帮助(即使在二维数组中排序似乎是一个非常有趣的问题)对所有行进行bubblesort,然后对所有列进行bubblesort,然后重复,直到没有冒泡为止。如果有一个一维的bubblesort函数,那么对二维数组进行排序意味着什么呢?
#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;
}