Skip to main content
added 1046 characters in body
Source Link
dbl
  • 141
  • 3

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 :

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 :

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 :

added 73 characters in body
Source Link
dbl
  • 141
  • 3
private static void fillSpiralMatrix(int[][] matrix, int n) {
    for (int step = 0, a = 0, size; step < n/2; step++) {
        size = (n - step * 2 - 1);
        for (int i = 0, chunk, chunkIndex, chunkOffset; i < 4 * (n - step * 2 - 1);size; i++) {
            chunk = i / size;
            chunkIndex = i % size;
            chunkOffset = n - step - 1;
            switch (chunk) {
                case 0:
                    matrix[step][chunkIndex + step] = a;
                    break;
                case 1:
                    matrix[chunkIndex + step][chunkOffset] = a;
                    break;
                case 2:
                    matrix[chunkOffset][chunkOffset - chunkIndex] = a;
                    break;
                case 3:
                    matrix[chunkOffset - chunkIndex][step] = a;
                    break;
                default:
                    throw new IndexOutOfBoundsException();
            }
            a++;
        }
        if (n % 2 == 1) {
            matrix[n/2][n/2] = n * n - 1;
        }
    }
}
private static void fillSpiralMatrix(int[][] matrix, int n) {
    for (int step = 0, a = 0, size; step < n/2; step++) {
        size = (n - step * 2 - 1);
        for (int i = 0, chunk, chunkIndex, chunkOffset; i < 4 * (n - step * 2 - 1); i++) {
            chunk = i / size;
            chunkIndex = i % size;
            chunkOffset = n - step - 1;
            switch (chunk) {
                case 0:
                    matrix[step][chunkIndex + step] = a;
                    break;
                case 1:
                    matrix[chunkIndex + step][chunkOffset] = a;
                    break;
                case 2:
                    matrix[chunkOffset][chunkOffset - chunkIndex] = a;
                    break;
                case 3:
                    matrix[chunkOffset - chunkIndex][step] = a;
                    break;
                default:
                    throw new IndexOutOfBoundsException();
            }
            a++;
        }
        if (n % 2 == 1) {
            matrix[n/2][n/2] = n * n - 1;
        }
    }
}
private static void fillSpiralMatrix(int[][] matrix, int n) {
    for (int step = 0, a = 0, size; step < n/2; step++) {
        size = (n - step * 2 - 1);
        for (int i = 0, chunk, chunkIndex, chunkOffset; i < 4 * size; i++) {
            chunk = i / size;
            chunkIndex = i % size;
            chunkOffset = n - step - 1;
            switch (chunk) {
                case 0:
                    matrix[step][chunkIndex + step] = a;
                    break;
                case 1:
                    matrix[chunkIndex + step][chunkOffset] = a;
                    break;
                case 2:
                    matrix[chunkOffset][chunkOffset - chunkIndex] = a;
                    break;
                case 3:
                    matrix[chunkOffset - chunkIndex][step] = a;
                    break;
                default:
                    throw new IndexOutOfBoundsException();
            }
            a++;
        }
        if (n % 2 == 1) {
            matrix[n/2][n/2] = n * n - 1;
        }
    }
}
added 73 characters in body
Source Link
dbl
  • 141
  • 3
private static void placeBoardersfillSpiralMatrix(int[][] matrix, int n) {
    int a = 0;

    for (int step = 0, a = 0, size; step < n/2; step++) {
        size = (n - step * 2 - 1);
        for (int i = 0, chunk, chunkIndex;chunkIndex, chunkOffset; i < 4 * (n - step * 2 - 1); i++) {
            chunk = i / size;
            chunkIndex = i % size;
            chunkOffset = n - step - 1;
            switch (chunk) {
                case 0:
                    matrix[step][chunkIndex + step] = a;
                    break;
                case 1:
                    matrix[chunkIndex + step][n - step - 1]step][chunkOffset] = a;
                    break;
                case 2:
                    matrix[n - step - 1][(n - step - 1)matrix[chunkOffset][chunkOffset - chunkIndex] = a;
                    break;
                case 3:
                    matrix[(n - step - 1)matrix[chunkOffset - chunkIndex][step] = a;
                    break;
                default:
                    throw new IndexOutOfBoundsException("kur");
            }
            a++;
        }
        if (n % 2 == 1) {
            matrix[n/2][n/2] = n * n - 1;
        }
    }
}

In this case we do define

private static void placeBoarders(int[][] matrix, int n) {
    int a = 0;

    for (int step = 0, size; step < n/2; step++) {
        size = (n - step * 2 - 1);
        for (int i = 0, chunk, chunkIndex; i < 4 * (n - step * 2 - 1); i++) {
            chunk = i / size;
            chunkIndex = i % size;
            switch (chunk) {
                case 0:
                    matrix[step][chunkIndex + step] = a;
                    break;
                case 1:
                    matrix[chunkIndex + step][n - step - 1] = a;
                    break;
                case 2:
                    matrix[n - step - 1][(n - step - 1) - chunkIndex] = a;
                    break;
                case 3:
                    matrix[(n - step - 1) - chunkIndex][step] = a;
                    break;
                default:
                    throw new IndexOutOfBoundsException("kur");
            }
            a++;
        }
        if (n % 2 == 1) {
            matrix[n/2][n/2] = n * n - 1;
        }
    }
}
private static void fillSpiralMatrix(int[][] matrix, int n) {
    for (int step = 0, a = 0, size; step < n/2; step++) {
        size = (n - step * 2 - 1);
        for (int i = 0, chunk, chunkIndex, chunkOffset; i < 4 * (n - step * 2 - 1); i++) {
            chunk = i / size;
            chunkIndex = i % size;
            chunkOffset = n - step - 1;
            switch (chunk) {
                case 0:
                    matrix[step][chunkIndex + step] = a;
                    break;
                case 1:
                    matrix[chunkIndex + step][chunkOffset] = a;
                    break;
                case 2:
                    matrix[chunkOffset][chunkOffset - chunkIndex] = a;
                    break;
                case 3:
                    matrix[chunkOffset - chunkIndex][step] = a;
                    break;
                default:
                    throw new IndexOutOfBoundsException();
            }
            a++;
        }
        if (n % 2 == 1) {
            matrix[n/2][n/2] = n * n - 1;
        }
    }
}

In this case we do define

added 1360 characters in body
Source Link
dbl
  • 141
  • 3
Loading
added 10 characters in body
Source Link
dbl
  • 141
  • 3
Loading
added 376 characters in body
Source Link
dbl
  • 141
  • 3
Loading
Source Link
dbl
  • 141
  • 3
Loading