简体   繁体   English

使用所有元素的二维数组排序

[英]two dimensional array sorting using all elements

I'm working on this code in my program right now and it seems that the problem is with the line where I stop the inner loop of the 2nd dimension. 我正在程序中处理此代码,似乎问题出在我停止第二维内循环的那一行。

this is a sample output of the array 这是数组的示例输出

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

when i run this code the output is: 当我运行此代码时,输​​出为:

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

my expected output is: 我的预期输出是:

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

a digit:"6" is not in the correct place. 一个数字:“ 6”不在正确的位置。 Its because when I try to run the part where there is a nested for loop above a for loop, it only runs once and so it only checks the 1st column instead of getting to the third column where 6 is. 这是因为当我尝试运行在for循环上方有嵌套for循环的部分时,它只运行一次,因此只检查第一列而不是到达第六列的第三列。 The problem is I need to limit that loop in only reading the highest numbers from row#0 column#0 to row#2 column#0. 问题是我只在从行#0列#0到行#2列#0中读取最高编号时才需要限制该循环。

How do I solve this problem?? 我该如何解决这个问题? I thought of using a one dimensional array and put all two dimensional array elements and sort it there then put it back to the two dimensional array and print it again but that wouldn't make my code solve the needed process of sorting two dimensional array. 我想到了使用一维数组并将所有二维数组元素放入其中并对其进行排序,然后将其放回到二维数组中并再次打印出来,但这不会使我的代码解决对二维数组进行排序所需的过程。

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);
}

The problem is in your loops where you search max element. 问题出在您搜索max元素的循环中。 Suppose you have array 5x5 and x=1 and y=1 . 假设您有数组5x5,并且x=1y=1 Then you loop will check only following elements: [0][0], [0][1], [1][0], [1][1]. 然后循环将仅检查以下元素:[0] [0],[0] [1],[1] [0],[1] [1]。 But it should also check [0][2], [0][3], [0][4]. 但它也应检查[0] [2],[0] [3],[0] [4]。

With you previous code you only checked following cells: 使用先前的代码,您仅检查了以下单元格:

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

But you need to check these: 但是您需要检查以下内容:

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

So you need something like this: 所以你需要这样的东西:

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;
        }
    }
}

My code checks every row fully until last one. 我的代码完全检查每一行,直到最后一行。

EDIT 编辑

If you use: 如果您使用:

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

then you iterate only on recangle with upper left corner in (0,0) and right bottom cornar in (y,x). 那么您只能在(0,0)中的左上角和(y,x)中的右下角膜上进行矩形迭代。 Eg x = 4, y = 3: 例如x = 4,y = 3:

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

But your goal is to do every row before last one fully. 但是您的目标是在最后一行完全之前完成每一行。 So check 0-th, 1-st and 2-nd rows fully and 3 elements from 3-rd row. 因此,请完全检查第0、1和2行,以及第3行中的3个元素。 My code does it. 我的代码做到了。 upper show how many values from row we need to check for all rows except last one it's equals to len - 1 (check full row). upper显示了我们需要检查的所有行中有多少个值,除了最后一个等于len - 1 (检查完整行),我们需要检查所有行。 For last one it's y . 最后一个是y

Your swap code (starting with int temp = twodiArray ) is outside the main iteration loop. 您的交换代码(以int temp = twodiArray )在主迭代循环之外。 It needs to be moved inside the innermost loop. 它需要在最里面的循环内移动。

BTW, you can do the swap without storing the indices. 顺便说一句,您可以进行交换而无需存储索引。

Personally, to save myself some confusion, I would think of it as if it were a 1D array. 就个人而言,为了避免混淆,我认为它好像是一维数组。

// 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--;
    }
}

You can see that I still use the 2D array and never use an actual 1D array, but this code indexes into the array as if it were a 1D array. 您可以看到我仍然使用2D数组,并且从不使用实际的1D数组,但是此代码将其索引为数组,就好像它是 1D数组一样。 (Hopefully that is valid in your professor's eyes) (希望这在您的教授看来是有效的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM