-4

I need help at sorting multidimensional array in ascending order.

So if input are:

2 5 6 1 4
5 9 2 1 3
7 4 2 4 5
9 2 5 8 5

I need the output:

1 1 2 2 2
2 3 4 4 4
5 5 5 5 5
6 7 8 9 9

And this is my mini version of the application, I stuck and don't even know how to search that, I have no idea how to push it, so I hope to seek help.

And This is my code:


        static void Main(string[] args)
        {

            int row = 4;
            int column = 5;

            int[,] mas = new int[row, column];
            InitMas(mas);
            PrintMas(mas);

        }
        static void InitMas(int[,] arr)
        {
            Random rn = new Random();
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    arr[i, j] = rn.Next(10, 100);
                }
            }

        }
        static void PrintMas(int[,] arr)
        {

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i, j] + " | ");
                }
                Console.WriteLine();
            }

        }
SomeBody
  • 7,515
  • 2
  • 17
  • 33
  • 2
    I can see you're assignment, but I'm not able to find what you've tried? Did you forgot to add it? Or what's the problem? We're not a solution provider. – Jeroen van Langen Jul 19 '21 at 11:23
  • I don't add it because I delite that part, I come whit hope that i get help here. – Iļja Teļnovs Jul 19 '21 at 11:25
  • 1
    make your 2dim array into a 1dim array, sort it, make it 2d again. – Patrick Artner Jul 19 '21 at 11:26
  • If you want us to be able to help you need to post your attempts, for now-> https://stackoverflow.com/questions/8866414/how-to-sort-2d-array-in-c-sharp – InUser Jul 19 '21 at 11:43
  • we help you by telling you what you did wrong, so that you can change it and solve your problem. If you haven't done anything we cannot tell you what you did wrong. – Mong Zhu Jul 19 '21 at 11:45

1 Answers1

1

You can flatten your array, sort the one-dimensional array and the make it twodimensional again. But actually, your two-dimensional array is stored in memory as a one-dimensional array, and the compiler calculates from row and column the one-dimensional index. Hence this transformation to one-dimensional first and then back to twodimensional is not necessary. If you use pointers, you can directly access the two-dimensional array in a one-dimensional way. Although unsafe code (which is necessary for pointers) is uncommon in C# and it shouldn't be used without good reasons, but you can gain here some performance.

This example uses only bubble sort, but it shows the idea:

public static unsafe void Sort(int[,] input)
{
    fixed(int* pointer = &input[0,0])
    {
        for(int i = 0; i < input.Length; i++)
        {
            for(int j = 0; j < input.Length - 1; j++)
            {
                if(*(pointer + j) > *(pointer + j + 1))
                {
                    int temp = *(pointer + j + 1);
                    *(pointer + j + 1) = *(pointer + j);
                    *(pointer + j) = temp;
                }
            }
        }
    }
}
SomeBody
  • 7,515
  • 2
  • 17
  • 33