首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在c#中对2D整型数组进行排序

问如何在c#中对2D整型数组进行排序
EN

Stack Overflow用户
提问于 2016-12-23 07:28:28
回答 2查看 316关注 0票数 0

我刚刚启动了c#,并找到了一个练习,该练习要求在不使用其他数组的情况下从小到大对数组中的数字进行排序,或者将其更改为一维数组。这就是我所做的,但它不起作用

​

​

代码语言:javascript
复制
     for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                Min = test[i, j];
                int o = 0;
                lign = i;
                col = j;
                for (int h = i; h < 3; h++)
                {

                    for (int z = j; z < 4; z++)
                    {
                        if (test[h, z] < Min)
                        {
                            Min = test[h, z];
                            lign = h;
                            col = z;
                            o = 1;
                        }
                    }
                }
                if (o == 1)
                {
                    temp = test[i, j];
                    test[i, j] = test[lign, col];
                    test[lign, col] = temp;
                }

            }
        }
EN

回答 2

Stack Overflow用户

发布于 2016-12-23 08:21:59

这应该能起到作用:

代码语言:javascript
复制
public int[,] Sort2DArray(int[,] input)
{
    int[] tempArray = new int[input.Length];
    Buffer.BlockCopy(input, 0, tempArray, 0, tempArray.Length * sizeof(int));

    Array.Sort(tempArray);

    int[,] output = new int[input.GetLength(0), input.GetLength(1)];
    Buffer.BlockCopy(tempArray, 0, output, 0, tempArray.Length * sizeof(int));

    return output;
}

Buffer.BlockCopy调用负责从二维数组到一维数组的转换,反之亦然。一旦将数组转换为一维,排序就很简单了。

票数 1
EN

Stack Overflow用户

发布于 2016-12-23 08:05:39

根据你的评论,我想对二维数组进行完全排序(而不是逐行排序)。

你必须像对待一维数组一样来处理你的二维数组。我使用的排序算法是简单的冒泡排序。但同样的逻辑也可以用于一些更快的算法,如快速排序

诀窍是将一维索引转换为二维索引。下面是根据列长度从一维索引中获取行和列的简单扩展方法。

代码语言:javascript
复制
/// <summary>
/// Calculates row and column index from given linear index and column length.
/// </summary>
/// <param name="index1D">linear index</param>
/// <param name="collength">column length</param>
/// <param name="row">returns index of row.</param>
/// <param name="col">returns index of column.</param>
public static void Get2DIndex(int index1D, int collength, out int row, out int col)
{
    row = index1D % collength;
    col = index1D / collength;
}

现在你要做的就是像普通数组一样迭代二维数组。您有一维索引,并使用Get2DIndex计算行和列

您有两个for循环来执行冒泡排序。

代码语言:javascript
复制
int[,] test = new int[4, 3]
{
    {45, 23, 9},
    {3, -4, -134},
    {67, 53, 32},
    {0, 1, 0}
};

// test.GetLength(n) will give the length at nth dimension.
// test.Length will give total length. (4*3)

var colLength = test.GetLength(1);

for (int i = 0; i < test.Length - 1; i++)
{
    for (int j = i + 1; j < test.Length; j++)
    {
        int row1, col1; // for first item
        int row2, col2; // next item

        Get2DIndex(i, colLength, out row1, out col1); // calculate indexes for first item

        Get2DIndex(j, colLength, out row2, out col2); // calculate indexes for next item

        if (test[col2, row2] < test[col1, row1]) // simple swap
        {
            var temp = test[col2, row2];
            test[col2, row2] = test[col1, row1];
            test[col1, row1] = temp;
        }
    }
}

打印结果:

代码语言:javascript
复制
for (int i = 0; i < test.GetLength(0); i++)
{
    for (int j = 0; j < test.GetLength(1); j++)
    {
        Console.Write("{0}\t", test[i, j]);
    }
    Console.WriteLine();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41293137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档