我试图在C#中插入排序算法,并努力修复该错误消息:不知道是什么意思IndexOutOfRangeException
“System.IndexOutOfRangeException”发生在algorithmsAssignment.exe”
只要它到达while循环,代码就会中断并给出消息。任何帮助,将不胜感激
(我不得不为我使用了一个二维数组字符串做string.compare
static void insertionSort(int columnSort, bool accendingOrder)
{
int column = columnSort - 1;
int i, j;
for (i = 1; i < dataArray.GetLength(1); i++)
{
string key = dataArray[column, i];
j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && string.Compare(dataArray[column, j - 1],
dataArray[j, column]) > 0)
{
dataArray[column, j + 1] = dataArray[column, j];
j = j - 1;
}
dataArray[column, j + 1] = key;
}
}
我不想告诫你,但我认为你很有可能使用小于零的指数,或者大于或等于第索引数组。 –
您是否试图在文档中查找它?这是很好描述https://msdn.microsoft.com/en-Us/library/system.indexoutofrangeexception(v=vs.110).aspx – derpirscher