java - How to group the rows from a multidimensional array of numbers? -
i've been trying sort multidimensional array without using array.sort
int arg[][] = { {26, 39, 3, 13}, {22, 97, 17, 123}, {46, 19, 63, 123}, {1, 37, 90, 32}, {17, 37, 90, 32}};
into this
int arg[][] = { {3, 13, 26, 39}, {17,22,97,123}, {19, 46, 63, 123}, {1, 32, 37, 90}, {17, 32, 37, 90}};
it sorts rows lowest number highest.
using bubble sort, have tweaked code , done sorts first 3 rows , , throws out of bound error.
this code have
for (int = arg[0].length - 1; >= 0; i--) { (int j = 0; j < ; j++) { (int k = 0; k < ; k++) { if (arg[k][j] > arg[k][j + 1]) { int temp = arg[k][j]; arg[k][j] = arg[k][j + 1]; arg[k][j + 1] = temp; } } } } (int = 0; < arg.length; i++) { (int j = 0; j < arg[i].length; j++) { system.out.print(arg[i][j] + " "); } system.out.println(); }
what doing wrong?
as said last 2 rows not sorted.i print out indexces iterating on showed me dint reach last 2 rows .instead can do
for (int = arg[0].length - 1; >= 0; i--) { (int j = 0; j < arg.length; j++) { (int k = 0; k < arg[j].length - 1; k++) { if (arg[j][k] > arg[j][k + 1]) { int temp = arg[j][k]; arg[j][k] = arg[j][k + 1]; arg[j][k + 1] = temp; } } } }
Comments
Post a Comment