Can someone show me algorithm so i can sort array like this?
Picture

Recommended Answers

All 7 Replies

Why not just sort the linear array by an algorith of choice (bubble, quick, etc.) then fill the 2-D array like that?

If you hate the temporary array, then just apply selection sort and swap with the indexes starting from rr[Len/2 -1][Len/2 -1]

One thing I might do is spread those out in numerical (or reverse numerical) order and look for a pattern.
If you look at this list, can you see two for loops?
0,0
0,1
0,2
0,3
0,4
0,5
1,5
2,5
3,5
4,5
5,5
5,4
5,3
5,2
5,1
5,0
4,0
3,0
2,0
1,0
1,1
1,2
1,3
1,4
2,4
3,4
4,4
4,3
4,2
4,1
3,1
2,1
2,2
2,3
3,3
3,2

And how is this sorted? I dont see any sortig there on that image.

Its only somekind of a table with numbers inisde - which start from the middle box and they go out?

If I understand you, you have an array of numbers, from 1 to 36, and you would like to show them in this kind of order?

Ok, I mess around a bit with the code you want to, and this is what came out.
I hope you like it:

public partial class Form1 : Form
{
    DataTable table;
    public Form1()
    {
        InitializeComponent();
        //I will create this code in form`s constructor, so you will see it when form shows up

        //creating and setting dgv:
        DataGridView dataGridView1 = new DataGridView();
        dataGridView1.Location = new Point(20, 20);
        this.Controls.Add(dataGridView1);
        dataGridView1.AutoSize = true;
        dataGridView1.AllowUserToAddRows = false;
        dataGridView1.RowHeadersVisible = false;

        //creating datatable:
        table = new DataTable();
        for (int i = 1; i < 7; i++)
        {
            table.Columns.Add(string.Format("c{0}", i), typeof(int));
            table.Rows.Add();
        }

        //main code for sorting numbers in your way:
        int[] nums = Enumerable.Range(1, 36).ToArray();
        int x = 2, y = 3;
        int[] steps = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5 };
        int number = 1;
        int side = 1;
        //sides: 1 = right, 2 = up, 3 = left, 4 = down

        //lets insert 1st starting number:
        InsertNumber(x, y, nums[0]);
        for (int i = 0; i < steps.Length; i++)
        {
            SettingSteps(steps[i], ref side, ref number, ref x, ref y);
            if (side == 4)
                side = 1;
            else
                ++side;
        }
        //setting datasource to datagridview to show result:           
        dataGridView1.DataSource = table.DefaultView;
        for (int i = 0; i < dataGridView1.Columns.Count; i++)
            dataGridView1.Columns[i].Width = 30;
    }

    private void SettingSteps(int steps, ref int side, ref int num, ref int x, ref int y)
    {
        for (int i = 0; i < steps; i++)
        {
            switch (side)
            {
                case 1: x++; break;
                case 2: y--; break;
                case 3: x--; break;
                case 4: y++; break;                  
            }
            InsertNumber(x, y, ++num);
        }
    }

    private void InsertNumber(int x, int y, int num)
    {
        table.Rows[y][x] = num;
    }
}

best regards,
bye

Sorry for the late reply, will try your code and let you know.
Thanks.

Thanks Mitja, it works perfectly.

It sure does (vote +1)
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.