我試圖在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