简体   繁体   中英

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

so i just started c# and found an exercise that says to sort the numbers in the array from small to big without using another array or change it to a 1D array this is what i did yet it does not work

在此处输入图片说明

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

            }
        }

This should do the trick:

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

The Buffer.BlockCopy calls take care of converting from a 2D array to a 1D array and vice versa. Once you've converted the array to 1D, sorting is trivial.

Based on your comment want to sort the 2d array completely (not row by row).

You have to tread your 2d array like 1d array. the sort algorithm I used is the simple bubble sort. but same logic can be used on some faster algorithms like quicksort

The trick is to convert a 1d index into 2d index. here is the simple extension method to get row and column from a 1d index based on column length.

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

Now all you have to do is to iterate on 2d array like a normal array. you have 1d index and you calculate row and column using Get2DIndex

you have two for loops to do your bubble sort.

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

Print the results:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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