0
我试图使用插入排序来排序Java中的每个行的第一列值的二维数组。我已经测试了它的大小为2的数组,但是当我尝试使用大小为3的代码时,它甚至不运行for循环。感谢您提供任何帮助。插入排序和2维数组
public int[][] sortC(int[][] temp)
{
if (temp.length == 1)
{
return temp;
}
else if (temp.length >= 2)
{
for (int i = 1; i <= temp.length - 1; i++)
{
int holdRow = temp[i][0];
int holdCol = temp[i][1];
// hold past index
int holdRowP = temp[i - 1][0];
int holdColP = temp[i - 1][1];
int j = i;
while (j > 0 && holdRow < holdRowP)
{
holdRow = temp[j][0];
holdCol = temp[j][1];
// hold past index
holdRowP = temp[j - 1][0];
holdColP = temp[j - 1][1];
// make single swap
temp[j][0] = holdRowP;
temp[j][1] = holdColP;
temp[j-1][0] = holdRow;
temp[j-1][1] = holdCol;
j--;
}
}
}
return temp;
}