I have 2D array which I need to sort it's rows only, and I have done with this code:
double[][] arr = initArray();
for (int row = 0; row < arr.length; row++) {
int index = row;
double minVal = arr[row][0];
for (int column = 0; column < arr[row].length; column++) {
if (arr[row][column] < minVal) {
minVal = arr[row][column];
index = column;
}
}
arr[row][index] = arr[row][0];
arr[row][0] = minVal;
}
Sample input:
{{0.15, 0.875, 0.375},
{0.55, 0.005, 0.225},
{0.30, 0.12, 0.4}}
Expect out put:
0.15 0.375 0.875
0.005 0.225 0.55
0.12 0.3 0.4
What I get:
0.15 0.875 0.375
0.005 0.55 0.225
0.12 0.3 0.4