2

I'm new here and sorry If my question is stupid, but I really need you help. I need to sort that two dimensional string array by id (the first column):

string [,] a = new string [,]
{
    {"2","Pena","pena"},
    {"1","Kon","kon"},
    {"5","Sopol","sopol"},
    {"4","Pastet","pastet"},
    {"7","Kuche","kuche"}
};

The problem is that I'm sorting only the number and I want after them to sort the words. That's what I did so far

static void Main(string[] args)
{
    string [,] a = new string [,]
    {
        {"2","Pena","pena"},
        {"1","Kon","kon"},
        {"5","Sopol","sopol"},
        {"4","Pastet","pastet"},
        {"7","Kuche","kuche"}
    };

    int b = a.GetLength(0);

    Console.WriteLine(b);
    Console.WriteLine(a[0,0]);
    Console.WriteLine(a[0,1]);
    Console.WriteLine(a[1,0]);

    InsertionSort(a, b);

    Console.WriteLine();
    Console.Write("Sorted Array: ");
    printArray(a);

    Console.WriteLine();
    Console.Write("Press any key to close");
    Console.ReadKey();

}

public static void InsertionSort(string[,] iNumbers, int iArraySize)
{
    int i, j, index;

    for (i = 1; i < iArraySize; i++)
    {
        for (int k = 0; k < iNumbers.GetLength(1); k++)
        {
            index = Convert.ToInt32(iNumbers[i, 0]);
            j = i;

            while ((j > 0) && (Convert.ToInt32(iNumbers[j - 1, 0]) > index))
            {
                iNumbers[j, k] = iNumbers[j - 1, k];
                j = j - 1;
            }

            iNumbers[j, 0] = Convert.ToString(index);
        }
    }
}


static void printArray(string[,] iNumbers)
{
    for (int i = 0; i < iNumbers.GetLength(0); i++)
    {
        for (int k = 0; k < iNumbers.GetLength(1); k++) 
        {
            Console.Write(iNumbers[i, k] + " ");

        }
    }
    Console.WriteLine();
}

Unfortunatelly as output I get

1 Pena pena 2 Kon kon 4 Sopol sopol 5 Pastet pastet 7 Kuche kuche

I would be really grateful if you could help me.

JustSomeGuy
  • 3,677
  • 1
  • 23
  • 31
Avtontom_
  • 305
  • 2
  • 6
  • 19
  • This looks like homework. It would be helpful if you could clarify the exact constraints for the problem. The main issue you have is that your `k` loop (which is not really part of the insertion sort per se) belongs inside the `j` loop (which is), not outside. In your code, by the time you try to sort anything other than the first element of a row, the index itself has been sorted and so your loop can't reorder the other parts. But the whole thing would be immune to that bug if you used a jagged array, or just indexed the original data. Are these approaches allowed by your teacher? – Peter Duniho Mar 01 '15 at 15:56

1 Answers1

2

Based on the nature of the example and the question, I am guessing that this is a homework assignment and so must be implemented in a fashion that is a) not far from your current example, and b) actually demonstrates an insertion sort.

With that in mind, the following is a corrected version of your example that works:

public static void InsertionSort(string[,] iNumbers, int iArraySize)
{
    int i, j, index;

    for (i = 1; i < iArraySize; i++)
    {
        index = Convert.ToInt32(iNumbers[i, 0]);
        j = i;

        while ((j > 0) && (Convert.ToInt32(iNumbers[j - 1, 0]) > index))
        {
            for (int k = 0; k < iNumbers.GetLength(1); k++)
            {
                string temp = iNumbers[j, k];

                iNumbers[j, k] = iNumbers[j - 1, k];
                iNumbers[j - 1, k] = temp;
            }

            j = j - 1;
        }
    }
}

I made two key changes to your original code:

  1. I rearranged the k and j loops so that the k loop is the inner-most loop, rather than the j loop. Your j loop is the one performing the actual sort, while the k loop is what should be actually moving a row for an insertion operation.

In your original example, you had this reversed, with the result that by the time you went to sort anything except the index element of a row, everything looked sorted to the code (because it's only comparing the index element) and so nothing else got moved.

With the above example, the insertion point is determined first, and then the k loop is used simply to do the actual insertion.

  1. I added logic to actually swap the elements. In your original code, there wasn't really a swap there. You had hard-coded the second part of a swap, simply copying the index element to the target, so the swap did work for the index element. But it wouldn't have achieved the swap for any other element; instead, you'd just have overwritten data.

With the above, a proper, traditional swap is used: one of the values to be swapped is copied to a temp local variable, the other value to be swapped is copied to the location of the first value, and then finally the saved value is copied to the location of the second.


The above should be good enough to get you back on track with your assignment. However, I will mention that you can get rid of the k loop altogether if your teacher will allow you to implement this using jagged arrays (i.e. a single-dimensional array containing several other single-dimensional arrays), or by using a second "index array" (i.e. where you swap the indexes relative to the original array, but leave the original array untouched).

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136