1

I'm trying to set up a method that will sort a two dimensional array of doubles by column. Based on the provided specifications, this method is also not supposed to take ragged arrays with rows of unequal lengths. I'm testing this using the double[][] mdarray = {{3.0, 4.0, 1.0, 8.0}, {13.0, 2.0, 12.0, 9.0}

Using a print method, this should be displayed as

3.0, 2.0, 1.0, 8.0,

13.0, 4.0, 12.0, 9.0,

When using a separate print method to output the result, the array appears to be unchanged. I think my problem is figuring out how to check every row for a given column before moving onto the next, but I'm not sure how to do that easily without the Arrays.sort() method, which is not allowed for this exercise.

public void sort(boolean byColumn)
{
    if(isRagged() == true)
    {
        System.out.println("Ragged arrays cannot be sorted by column.");
    }
    else
    {
        for(int i = 0; i < mdarray.length; i++)
        {
            for(int j = i + 1; j < mdarray.length - 1; j ++)
            {
                for(int k = 0; k < mdarray[i].length; k++)
                {
                    if(mdarray[i][k] > mdarray[j][k])
                    {
                        double temp = mdarray[i][k];
                        mdarray[i][k] = mdarray[j][k];
                        mdarray[i][k] = temp;
                    }
                }
            }
        }
    }
}
5
  • mdarray[i][k] = temp; should be mdarray[i][j] = temp; Commented Dec 8, 2016 at 7:38
  • I just tried that. The output is still the same. Commented Dec 8, 2016 at 7:41
  • @shmosel you probably meant it's supposed to be mdarray[j][k] = temp Commented Dec 8, 2016 at 7:58
  • @radoh Oops, yes. Commented Dec 8, 2016 at 7:58
  • I switched it to what you suggested. The original array didn't change still, but a separate test array {{1.0, 5.0, 7.0}, {4.0, 1.0, 3.0}, {9.0, 2.0, 8.0} changed to {{1.0, 1.0, 3.0}, {4.0, 5.0, 7.0}, {9.0, 2.0, 8.0}. It's definitely closer. Commented Dec 8, 2016 at 8:04

2 Answers 2

1

You've actually switched the bounds of i and j variables as well. Since j iterates from i+1, it should gain values up to mdarray.length - 1, therefore the condition should be j < mdarray.length and similarly i's upper bound should be mdarray.length - 2 therefore the condition should be i < mdarray.length - 1.

Oh and also there's the mistake we mentioned in comments - when switching the elements, assign the temp variable to mdarray[j][k].

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

Comments

0

As you know you can consider a 2D array as a group of columns or rows. This program has to sort columns, so we'll consider the array as a group of columns.

The program is done:

  1. loops all columns
  2. choose your favorite algorithm for sorting a 1D array (take a look here)

    double[][] mdarray = {{3.0, 4.0, 1.0, 8.0}, {13.0, 2.0, 12.0, 9.0}};
    
    //loop all columns
    for(int col = 0; col < mdarray[0].length; col++){
    
        //loops all rows and use bubble sort algorithm in ascending order
        for(int i = 1; i < mdarray.length; i++){
    
            for(int j = i; j < mdarray.length; j++){
                if(mdarray[j - 1][col] > mdarray[j][col]){
                    //swap
                    double temp = mdarray[j][col];
                    mdarray[j][col] = mdarray[j-1][col];
                    mdarray[j-1][col] = temp;
                }
            }
        }
    }
    

If you print each row of mdarray, the output will be:

mdarray[0]: 3.0  2.0 1.0  8.0
mdarray[1]: 13.0 4.0 12.0 9.0 

Comments

Your Answer

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