and even add some more clarity I will define some variables so the things get a bit clear for you:
private static void placeBoarders(int[][] matrix, int n) {
for (int i = 0, size = (n - 1), a = 0, chunk, chunkIndex; i < 4 * size; i++) {
chunk = i / size;
chunkIndex = i % size;
switch (chunk) {
case 0:
matrix[0][chunkIndex] = a;
break;
case 1:
matrix[chunkIndex][size] = a;
break;
case 2:
matrix[size][size - chunkIndex] = a;
break;
case 3:
matrix[size - chunkIndex][0] = a;
break;
default:
throw new IndexOutOfBoundsException();
}
a++;
}
}
Here instead of using matrix[X][Y] = a, you should use a as index for the sorted B array so it will be like matrix[X][Y] = B[a];
If you need to take the indexes of the the matrix starting from (0,0) in the direction ( top left -> top right -> bottom right -> bottom left -> top right) then you could use this code :