0

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);
}
3
  • 1
    Do you really need the array to be 2D? It seems like you just want a sort on a 1D array. Commented Jan 8, 2013 at 15:31
  • Yes it is required to be 2D, at first I thought it was really just a 1d process and do it there but the professor wants us to solve this problem this way. He is the boss. But if i don't find any solution done in an hour I'm going to call it quits and do the 1D process. Commented Jan 8, 2013 at 15:33
  • why is it required? why can't you collapse { 4, 5, 6 } into 456 and treat it as a 1D array? Commented Jan 8, 2013 at 15:43

3 Answers 3

1

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). E.g. 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.

Sign up to request clarification or add additional context in comments.

8 Comments

can you please elaborate or explain to me what this code does?
@davidgpayne I modified my answer.
Thanks it worked! But I still don't understand how the code works exactly(the value you insert to the variable named upper) I really wanna know how it gets the last item o.O really appreciate it if u can tell me how that works
I understand now how my code is wrong ... hm but how the integer upper gets to make it right still eludes me >.> I've never encountered an integer value with operators/symbols like the question mark and the colon.
@davidgpayne ah, sorry. Google for ternary operator in java. It is just condition statement. I have replaced it with normal condition in my answer.
|
0

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.

1 Comment

i don't think there is a problem with my swap code, i am more comfortable with saving the indices and it being outside the loops coz the loops are only there so i can read the highest values from some range within the 2d array
0

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)

1 Comment

I will study this later, saved file. But at the moment the one I made with the help of Sir Nikita is fully working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.