简体   繁体   中英

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
  • 7 6 4
  • 4 8 5

when i run this code the output is:

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

my expected output is:

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

a digit:"6" is not in the correct place. 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. 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.

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. Suppose you have array 5x5 and x=1 and y=1 . Then you loop will check only following elements: [0][0], [0][1], [1][0], [1][1]. But it should also check [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). Eg 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. 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). For last one it's y .

Your swap code (starting with int temp = twodiArray ) is outside the main iteration loop. 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. (Hopefully that is valid in your professor's eyes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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