0

I have been struggling to do some sorting algorithms while using a 2D array. The issue I'm having is im getting the error message:

System.IndexOutOfRangeException occurred in algorithmsAssignment.exe

Any help would be appreciated.

public static void selectionSort(string[,] dataArray, int columnSort)
{
        int column = columnSort - 1;
        int pos_min;

        for (int i = 0; i < dataArray.GetLength(1) - 1; i++)
        {
            pos_min = i;    // set pos_min to the current index of array

            for (int j = i + 1; j < dataArray.GetLength(1); j++)
            {
                if (string.Compare(dataArray[j, column] , dataArray[pos_min, 
column]) > 0 )
                {
                    //pos_min will keep track of the index that min is in, 
this is needed when a swap happens
                    pos_min = j;
                }
            }

            //if pos_min no longer equals i than a smaller value must have 
been found, so a swap must occur
            if (pos_min != i)
            {
                string temp = dataArray[i, column];
                dataArray[i, column] = dataArray[pos_min, column];
                dataArray[pos_min, column] = temp;
            }
        }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

0 Answers0