简体   繁体   中英

Java: Sorting a 2D Array by Columns without Arrays.sort()

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

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] .

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 

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