我怎样才能在c#中对2D int数组进行排序

时间:2016-12-22 23:28:28

标签: c# arrays

所以我刚刚开始使用c#并找到一个练习,它说要将数组中的数字从小到大排序而不使用另一个数组或将其更改为1D数组这就是我所做的但它不起作用

enter image description here

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

            }
        }

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

g<int>

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; } 调用负责从2D数组转换为1D数组,反之亦然。一旦将数组转换为1D,排序就很简单了。

答案 1 :(得分:0)

根据您的评论想要完全排序2d数组(不是逐行)。

你必须像1d阵列一样踏上你的2D阵列。我使用的排序算法是简单的冒泡排序。但是同样的逻辑可以用在一些更快的算法上,比如quicksort

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

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