簡體   English   中英

如何對二維數組的列進行排序?

[英]How I can sort two dimensional array's columns?

大家好,我想對二維數組的列進行排序。我想從用戶那里獲取數組的維度和元素並將其顯示為矩陣。 然后減去同一個數組的排序形式。 我們只需要對數組的列進行排序。 請幫忙。

類似的東西

{{0, 1, 3},
{6, 0, 8},
{5, 9, 2}}


{{0, 0, 2},
{5, 1, 3},
{6, 9, 8}}

為此,我在 C ++ 中編寫了代碼,我可以對數組的第一列進行排序並顯示它,但我不能做其他列。 為此我向你呼吁。

#include <iostream>
using namespace std;
int main()
{
    int column, row;
    cout << "Column = ";
    cin >> column;
    cout << "Row = ";
    cin >> row;
    int array[column][row];
    int sortedarray[column];
    for (int z = 0; z < column; z++) {
        for (int a = 0; a < row; a++) {
            cin >> array[z][a];
        }
    }
    for (int i = 0; i < column; i++) {
        sortedarray[i] = array[i][0];
    }

    cout << "\n";
    for (int y = 0; y < column; y++) {
        for (int i = 0; i < row; i++) {
            cout << array[y][i] << "    ";
        }
        cout << endl;
    }
    cout << "\n";

    int temp = 0;
    for (int i = 1; i < column; i++)
        for (int j = 0; j < column - i; j++) {
            if (sortedarray[j] > sortedarray[j + 1]) {
                temp = sortedarray[j];
                sortedarray[j] = sortedarray[j + 1];
                sortedarray[j + 1] = temp;
            }
        }
    cout << "COUT sorted array \n ";
    for (int i = 0; i < column; i++) {
        cout << sortedarray[i] << "  ";
    }
}

據我了解,您需要以下內容:

1 5 6

2 8 4

9 7 3

被分類為:

1 2 3

4 5 6

7 8 9

最簡單的方法是您必須將二維數組 map 轉換為一維數組 - “1 5 6 2 8 4 9 7 3”,使用最佳算法對它們進行排序。 這將返回“1 2 3 4 5 6 7 8 9”,然后將 map 從一維數組返回到二維數組。

實際上,您可以實現任何排序,這僅取決於您的映射。

你甚至可以實現這樣的目標

1 4 7

2 5 8

3 6 9

您需要的是從二維到一維的映射。

@Farhad 您可以將二維數組轉換為一維數組,如下所示:

array_1d = array_2d.flatten()

打印(array_1d)

希望這會有所幫助!

#將二維數組轉換為一維數組,如下所示:

array_1d = array_2d.flatten()

打印(array_1d)

這是我的解決方案:

#include <iostream>
#include <iomanip>
#include <vector>
#include <tuple>


int getDigitCount( int num )
{
    num = abs( num );

    return ( num < 10    ? 1 :
           ( num < 100    ? 2 :
           ( num < 1000    ? 3 :
           ( num < 10'000   ? 4 :
           ( num < 100'000   ? 5 :
           ( num < 1'000'000  ? 6 :
           ( num < 10'000'000  ? 7 :
           ( num < 100'000'000  ? 8 :
           ( num < 1'000'000'000 ? 9 :
                                    10 )))))))));
}

void printMatrix( const std::vector<int>& array,
                 const std::tuple< const size_t, const size_t >& dimensions, const int maxDigitCount )
{
    std::cout << '\n' << "    \\ Column  ";

    for ( size_t colNumber = 0; colNumber < std::get<1>( dimensions ); ++colNumber )
    {
        std::cout << std::left << std::setw( maxDigitCount + 2 ) << std::setfill(' ') << colNumber;
    }

    std::cout << '\n' << "Row  \\" << '\n' << '\n';

    for ( size_t idx = 0; idx < std::get<0>( dimensions ) * std::get<1>( dimensions ); ++idx )
    {
        if ( ( idx ) % ( std::get<1>( dimensions ) ) == 0 )
        {
            std::cout << " " << ( idx ) / ( std::get<1>( dimensions ) ) << "            ";
        }

        std::cout << std::left << std::setw( maxDigitCount + 2 ) << std::setfill(' ') << array[ idx ];

        if ( ( idx + 1 ) % ( std::get<1>( dimensions ) ) == 0 )
        {
            std::cout << '\n' << '\n';
        }
    }
}

int main()
{
    int inputRowCount { };
    int inputColCount { };

    // get row count and column count from the user

    do
    {
        std::cout << "Enter row count: ";
        std::cin >> inputRowCount;

        std::cout << "Enter column count: ";
        std::cin >> inputColCount;

    } while ( inputRowCount < 1 || inputColCount < 1 );

    const size_t rowCount { static_cast<unsigned int>( inputRowCount ) };
    const size_t colCount { static_cast<unsigned int>( inputColCount ) };
    const size_t elementCount { rowCount * colCount };

    std::vector<int> array( elementCount ); // the 1D array for storing the user's matrix

    // get user input ( user's matrix ) and populate the array

    int maxDigitCount { };

    for ( std::vector<int>::iterator it = array.begin( ); it != array.end( ); ++it )
    {
        std::cin >> *it;

        if ( getDigitCount( *it ) > maxDigitCount )
        {
            maxDigitCount = getDigitCount( *it );
        }
    }

    std::cout << "\nOriginal 2D array:\n"; // print out the array

    printMatrix( array, std::make_tuple< const size_t, const size_t >
                ( std::move( rowCount ), std::move( colCount ) ), maxDigitCount );

    // bubble sort the array

    for ( size_t iterCount = 0; iterCount < elementCount - 1; ++iterCount )
    {
        for ( size_t idx = 0; idx < ( elementCount - 1 - iterCount  ); ++idx )
        {
            if ( array[ idx ] > array[ idx + 1 ] )
            {
                int temp = array[ idx ];
                array[ idx ] = array[ idx + 1 ];
                array[ idx + 1 ] = temp;
            }
        }
    }

    std::cout << "\nSorted 2D array:\n"; // print out the sorted array

    printMatrix( array, std::make_tuple< const size_t, const size_t >
                ( std::move( rowCount ), std::move( colCount ) ), maxDigitCount );

    // transpose 1D array and store in 1D transposedArray

    size_t row { };
    size_t col { };

    std::vector<int> transposedArray( elementCount );

    for ( size_t idx = 0; idx < elementCount; ++idx )
    {
        if ( col == colCount )
        {
            ++row;
            col = 0;
        }

        size_t newIdx { row + ( col * rowCount ) };

        transposedArray[ newIdx ] = array[ idx ];

        ++col;
    }

    std::cout << "\nTransposed sorted 2D array:\n"; // print out the transposed sorted array

    printMatrix( transposedArray, std::make_tuple< const size_t, const size_t >
                ( std::move( colCount ), std::move( rowCount ) ), maxDigitCount );

    return 0;
}

還有一個樣品 output:

Enter row count: 5
Enter column count: 4
1  6  11  16
2  7  12  17
3  8  13  18
4  9  14  19
5  10 15  20

Original 2D array:

    \ Column  0   1   2   3
Row  \

 0            1   6   11  16

 1            2   7   12  17

 2            3   8   13  18

 3            4   9   14  19

 4            5   10  15  20


Sorted 2D array:

    \ Column  0   1   2   3
Row  \

 0            1   2   3   4

 1            5   6   7   8

 2            9   10  11  12

 3            13  14  15  16

 4            17  18  19  20


Transposed sorted 2D array:

    \ Column  0   1   2   3   4
Row  \

 0            1   5   9   13  17

 1            2   6   10  14  18

 2            3   7   11  15  19

 3            4   8   12  16  20

轉置的排序二維數組是您想要獲得的 output (基於我對您問題的理解)。

此外,頂部有兩個函數, getDigitCount用於計算一個數字在所有用戶提供的數字之間的最大位數以用於格式化目的(使用 std::setw)和另一個 function printMatrix用於打印矩陣。

我希望這個程序能產生你需要的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM