繁体   English   中英

我如何在C#中对2D int数组进行排序

[英]how can i sort a 2D int array in c#

所以我刚开始C#,发现一个练习,说不使用另一个数组就将数组中的数字从大到小排序,或者将其更改为一维数组,这是我所做的,但是它不起作用

在此处输入图片说明

     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;
                }

            }
        }

这应该可以解决问题:

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调用负责将2D数组转换为1D数组,反之亦然。 将数组转换为一维后,排序变得很简单。

根据您的评论,希望对2d数组进行完全排序(而不是逐行)。

您必须像2D数组那样踩2D数组。 我使用的排序算法是简单的冒泡排序。 但是相同的逻辑可以用于一些更快的算法,例如quicksort

诀窍是将1d索引转换为2d索引。 这是一种简单的扩展方法,可基于列长度从一维索引中获取行和列。

/// <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;
}

现在,您要做的就是像普通数组一样在2d数组上进行迭代。 您有1d索引,并使用Get2DIndex计算行和列

您有两个for循环可进行气泡排序。

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;
        }
    }
}

打印结果:

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM