对 2D 动态数组进行排序 C++

Sorting 2D Dynamic Array C++

本文关键字:排序 C++ 数组 2D 动态      更新时间:2023-10-16

当第 1 行用于产品 ID 且第 2 行用于产品价格时,我正在尝试对二维动态数组进行排序。我想按产品 ID 排序,并以宽度 5 格式显示结果。这是我的代码:

这部分很好,可以满足我的需求:

void readData (int**, int, int);
void printData(int**, int, int);
void sortbyPartID(int**, int, int);
int main()
{
    int index;
    int **PriceSheet, rows, columns;
    cout << "Enter the number of Products, and then the number of values associated with the products:  ";
    cout << "For default values, enter 5 (FIVE ITEMS, and enter 2 (TWO Values: ID and PRICE). ";
    cin >> columns >> rows;
    cout << endl;
    PriceSheet = new int* [rows];
    for (int row = 0; row < rows; row++)
        PriceSheet [row] = new int[columns];
    readData (PriceSheet, rows, columns);
    cout << endl;
    printData(PriceSheet, rows, columns);
    sortbyPartID(PriceSheet, rows, columns);
    return 0;
}
void readData (int **p, int rowSize, int colSize)
{
    for (int row = 0; row < rowSize; row++)
    {
        cout << "Row ZERO is the Product ID and Row 1 is the Product Pricen";
        cout << "Enter " << colSize << " numbers for the row number " << row << ": ";
        for (int col = 0; col < colSize; col++)
        cin >> p[row][col];
        cout << endl;
    }
}
void printData (int **p, int rowSize, int colSize)
{
    cout << "nnThese are the Products IDs and Prices as entered in the system:n";
    for (int row = 0; row < rowSize; row++)
    {
        for (int col = 0; col < colSize; col++)
            cout << setw(5) << p[row][col];
        cout << endl;
    }
}

本节是我需要帮助的地方

它可以正确读取并正确打印未排序的数组,但是我想不出对数组进行排序的方法。具体来说,我需要有关void sortbyPartID函数的帮助。我想使用气泡排序,但我不知道如何让这个函数工作。任何有关排序功能/算法的帮助将不胜感激。

void sortbyPartID (int **p, int rowSize, int colSize)
{
    int swap = -1;
    int end = colSize;
    int sortedID = **p;
    cout << "nnThese are the Products sorted Products IDs:n";
    for (int counter = colSize -1; counter >= 0; counter --)
        for (int index = 0; index < end ; index ++)
        {
            if (sortedID[index] > sortedID[index + 1])
            {
                swap = *sortedID[index + 1];
                sortedID[index + 1] = sortedID[index];
                *sortedID[index] = swap;
            }
        }
    for(int index = 0; index < end; index++)
    {
        cout << sortedID[index] << ", ";
    }
    cout << endl;
    end --;
}

当我跑步时,我在最后一部分得到了一些奇怪的结果。也许我错过了一些简单的东西,不确定。

我们也可以使用 do-while 执行此操作,如下所示:

bool isSwaped;
do
{
    isSwaped = false;
    for (int index = 0; index < end - 1 ; ++index)
    {
        if (p[index][0] > p[index + 1][0])
        {
            int swap = p[index + 1][0];
            p[index + 1][0] = p[index][0];
            p[index][0] = swap;
            isSwaped = true;
        }
    }
} while (isSwaped);

您可以通过使用对象来简化整个事情。对象允许您以理智的方式处理相关数据。还强烈推荐使用向量而不是 C 数组。

struct Product {
  int id;
  int price;
  vector<int> others;
}

然后,您可以将产品存储在vector<Product> my_products;中,然后对所有内容进行分类

std::sort(my_products.begin(), my_products.end(),
          [](const Product& a, const Product& b) { return a.id < b.id; });

您可以保留现有的输入/输出格式,但将值放在正确的位置。这样几乎不可能弄乱属性,一切都很容易使用。

int sortedID = **p;不是

你想要的,应该删除。(我想你想要int** sortedID = p;

您的气泡排序应该是这样的:

for (int counter = colSize -1; counter >= 0; --counter)
{
    for (int index = 0; index < end - 1 ; ++index)
    {
        if (p[index][0] > p[index + 1][0])
        {
            // std::swap(p[index], p[index + 1]);
            int* swap = p[index + 1];
            p[index + 1] = p[index];
            p[index] = swap;
        }
    }
}

现场演示