0

我现在正在我的程序中处理这段代码,似乎问题出在我停止第二维内部循环的那一行。

这是数组的示例输出

  • 9 6 6
  • 7 6 4
  • 4 8 5

当我运行此代码时,输​​出是:

  • 4 4 6
  • 5 6 6
  • 7 8 9

我的预期输出是:

  • 4 4 5
  • 6 6 6
  • 7 8 9

一个数字:“6”不在正确的位置。这是因为当我尝试运行在 for 循环上方有嵌套 for 循环的部分时,它只运行一次,因此它只检查第一列而不是到达 6 所在的第三列。问题是我需要限制该循环只读取从row#0 column#0到row#2 column#0的最高数字。

我该如何解决这个问题?我想过使用一维数组并将所有二维数组元素放入其中并对其进行排序,然后将其放回二维数组并再次打印,但这不会使我的代码解决对二维数组进行排序所需的过程。

public static void sortArray(){
    int x = len-1, y = len-1;
    int iKey=0,jKey=0;
    int cnt=0;
    do{
        cnt++;
        if(y==-1){
            x--;
            y=len-1;
        }
        System.out.println(cnt+".)"+x+"-"+y);
        int hi = -1;
        for(i = 0;i <= x; i++)
            for(j = 0;j <= y; j++){
                if(twodiArray[i][j]>hi){
                    hi = twodiArray[i][j];
                    iKey = i;
                    jKey = j;
                }
            }

        int temp = twodiArray[iKey][jKey];
            twodiArray[iKey][jKey] = twodiArray[x][y];
            twodiArray[x][y] = temp;
            //dispArray();
        y--;
    }while(cnt<9);
}
4

3 回答 3

1

问题出在您搜索最大元素的循环中。假设您有数组 5x5 和x=1y=1。然后你循环将只检查以下元素:[0][0]、[0][1]、[1][0]、[1][1]。但它也应该检查 [0][2]、[0][3]、[0][4]。

使用您之前的代码,您只检查了以下单元格:

XX...
XX...
.....
.....
.....

但是你需要检查这些:

XXXXX
XX...
.....
.....
.....

所以你需要这样的东西:

for(i = 0;i <= x; i++) {
    int upper; // How many elements we need to check on current row.
    if (i != x) {
       upper = len - 1; // We are not in last row, so check all elements.
    } else {
       upper = y; // On the last row we need to check only elements up to y.
    }
    for(j = 0;j <= upper; j++){
        if(twodiArray[i][j]>hi){
            hi = twodiArray[i][j];
            iKey = i;
            jKey = j;
        }
    }
}

我的代码完全检查每一行,直到最后一行。

编辑

如果您使用:

for (int i = 0; i <= x; i++) {
    for (int j = 0; j <= y; j++) {
        ...
    }
}

然后你只在(0,0)中左上角和(y,x)中右下角的recangle上迭代。例如 x = 4,y = 3:

XXX...
XXX...
XXX...
XXX...
......

但是您的目标是在最后一行之前完成每一行。因此,完全检查第 0 行、第 1 行和第 2 行以及第 3 行中的 3 个元素。我的代码做到了。upper显示我们需要检查除最后​​一行之外的所有行中的多少值len - 1(检查整行)。最后一个是y.

于 2013-01-08T15:47:40.750 回答
0

就个人而言,为了避免混淆,我会将其视为一维数组。

// I'm assuming that columnCount and rowCount are stored somewhere
public int getNthElement(int index) {
    int colIndex = index % columnCount;
    int rowIndex = (index - colIndex) / rowCount;
    return twodiArray[rowIndex][colIndex];
}

public void setNthElement(int index, int value) {
    int colIndex = index % columnCount;
    int rowIndex = (index - colIndex) / rowCount;
    twodiArray[rowIndex][colIndex] = value;
}

public void sortArray(int[][] array) {
    int elementCount = rowCount * columnCount;
    int curIndex = elementCount - 1;

    while (curIndex >= 0) {
        int highestIndex = -1;
        int highestValue = 0;

        for (int i = 0; i <= curIndex; i++) {
            int nthValue = getNthElement(i);
            if (nthValue > highestValue) {
                highestIndex = i;
                highestValue = nthValue;
            }
        }

        int swapValue = getNthElement(curIndex);
        setNthElement(curIndex, highestValue);
        setNthElement(highestIndex, swapValue);

        curIndex--;
    }
}

您可以看到我仍然使用 2D 数组并且从未使用实际的 1D 数组,但是这段代码索引到数组中,就好像它是1D 数组一样。(希望这在你的教授眼中是有效的)

于 2013-01-08T16:17:46.290 回答
0

您的交换代码(以 开头int temp = twodiArray)位于主迭代循环之外。它需要移动到最里面的循环内。

顺便说一句,您可以在不存储索引的情况下进行交换。

于 2013-01-08T15:51:45.297 回答