0

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

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

            }
        }
4
  • 2
    Define "does not work". Commented Dec 22, 2016 at 23:30
  • The DEBUGGER is your best friend in this case Commented Dec 22, 2016 at 23:39
  • how do you want to sort. only sort each row separately? or sort it without considering rows? Commented Dec 22, 2016 at 23:40
  • I need to sort the whole array like test[0,0]gets the smallest number and test[3,4] takes the biggest number Commented Dec 22, 2016 at 23:42

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

+1 but u want to understand where is the problem in my code it's logical and i am confused why it is not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.