Sort 2d array, which way is correct?

41 views Asked by At

Im about to sort a 2d array. However there is a certain way of doing this. (Times of running it through, before proceeding to next row)

As you can see, what i've done here, is that im first sorting row 0, whereafter im incrementing! Before i increment the row, i let it sort the columns math.pow(m[j].length, 2).. Is this correct?

Furthermore, is there a smart way of printing out a 2D array from a void method, without using a for loop ?

  for (int j = 0; j < m.length; j++)
  {

    for (k = 0; k < math.pow(m[j].length, 2); INCREMENT++)
    {  
        for (int i = 0; i < m[j].length; i++)
        {

        if (m[i][j] > m[i + 1][j]) 
        {
           int temp = m[i][j];
           m[i][j] = m[i + 1][j];
           m[i + 1][j] = m[i][j]
        }
        }
    }

  } 
0

There are 0 answers