Spiral filling an existing matrix in their respective positions in descending order efficiently

 Clash Royale CLAN TAG#URR8PPP
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
Question:
- Sort the boundary elements in descending order using any standard sorting technique and rearrange them in the matrix. 
- Calculate the sum of the boundary elements. 
- Display the original matrix, rearranged matrix and sum of the boundary elements. 
Code:
import java.util.*;
class SortBoundary
 int A, B, m, n; 
 static int sum=0;
 void input() //Function for taking all the necessary inputs
 m>10)
 
 System.out.println("Invalid Range");
 System.exit(0);
 
 else
 
 A = new int[m][m];
 n = m*m;
 B = new int[n]; // 1-D Array to store Boundary Elements
 System.out.println("Enter the elements of the Matrix : ");
 for(int i=0;i<m;i++)
 
 for(int j=0;j<m;j++)
 
 System.out.print("Enter a value : ");
 A[i][j]=sc.nextInt();
 
 
 
 
 /* The below function is used to store Boundary elements 
 * from array A to array B 
 */
 void convert()
 
 int x=0;
 for(int i=0;i<m;i++)
 
 for(int j=0;j<m;j++)
 i == m-1 
 
 
 void sortArray() //Function for sorting Boundary elements stored in array B
 
 int c = 0;
 for(int i=0; i<n-1; i++)
 
 for(int j=i+1; j<n; j++)
 
 if(B[i]<B[j]) // for ascending use B[i]>B[j]
 
 c = B[i];
 B[i] = B[j];
 B[j] = c;
 
 
 
 
 /* Function fillSpiral is filling the boundary of 2-D array in spiral
 * way from the elements of 1-D array
 */
 void fillSpiral()
 
 int R1=0, R2=m-1, C1=0, C2=m-1, x=0;
 for(int i=C1;i<=C2;i++) // accessing the top row
 
 A[R1][i]=B[x++];
 
 for(int i =R1+1;i<=R2;i++) // accessing the right column
 
 A[i][C2]=B[x++];
 
 for(int i =C2-1;i>=C1;i--) // accessing the bottom row
 
 A[R2][i]=B[x++];
 
 for(int i =R2-1;i>=R1+1;i--) // accessing the left column
 
 A[i][C1]=B[x++];
 
 
 void printArray() //Function for printing the array A
 
 for(int i=0;i<m;i++)
 
 for(int j=0;j<m;j++)
 
 System.out.print(A[i][j]+"t");
 
 System.out.println();
 
 
 public static void main(String args)
 
 SortBoundary ob = new SortBoundary();
 ob.input();
 System.out.println("*********************");
 System.out.println("The original matrix:");
 System.out.println("*********************");
 ob.printArray(); //Printing the original array
 ob.convert(); //Storing Boundary elements to a 1-D array
 ob.sortArray(); //Sorting the 1-D array (i.e. Boundary Elements)
 ob.fillSpiral(); //Storing the sorted Boundary elements back to original 2-D array
 System.out.println("*********************");
 System.out.println("The Rearranged matrix:");
 System.out.println("*********************");
 ob.printArray(); //Printing the rearranged array
 System.out.println("*********************");
 System.out.println("The sum of boundary elements is = "+sum); //Printing the sum of boundary elements
 
What I want to accomplish:
I want to fill the original array A with the sorted boundary elements B in a single loop at their respective positions in descending order.In my code I have used 4 different loops for accomplishing this task.Any suggestions or help to make my code better?
java array matrix
add a comment |Â
up vote
2
down vote
favorite
Question:
- Sort the boundary elements in descending order using any standard sorting technique and rearrange them in the matrix. 
- Calculate the sum of the boundary elements. 
- Display the original matrix, rearranged matrix and sum of the boundary elements. 
Code:
import java.util.*;
class SortBoundary
 int A, B, m, n; 
 static int sum=0;
 void input() //Function for taking all the necessary inputs
 m>10)
 
 System.out.println("Invalid Range");
 System.exit(0);
 
 else
 
 A = new int[m][m];
 n = m*m;
 B = new int[n]; // 1-D Array to store Boundary Elements
 System.out.println("Enter the elements of the Matrix : ");
 for(int i=0;i<m;i++)
 
 for(int j=0;j<m;j++)
 
 System.out.print("Enter a value : ");
 A[i][j]=sc.nextInt();
 
 
 
 
 /* The below function is used to store Boundary elements 
 * from array A to array B 
 */
 void convert()
 
 int x=0;
 for(int i=0;i<m;i++)
 
 for(int j=0;j<m;j++)
 i == m-1 
 
 
 void sortArray() //Function for sorting Boundary elements stored in array B
 
 int c = 0;
 for(int i=0; i<n-1; i++)
 
 for(int j=i+1; j<n; j++)
 
 if(B[i]<B[j]) // for ascending use B[i]>B[j]
 
 c = B[i];
 B[i] = B[j];
 B[j] = c;
 
 
 
 
 /* Function fillSpiral is filling the boundary of 2-D array in spiral
 * way from the elements of 1-D array
 */
 void fillSpiral()
 
 int R1=0, R2=m-1, C1=0, C2=m-1, x=0;
 for(int i=C1;i<=C2;i++) // accessing the top row
 
 A[R1][i]=B[x++];
 
 for(int i =R1+1;i<=R2;i++) // accessing the right column
 
 A[i][C2]=B[x++];
 
 for(int i =C2-1;i>=C1;i--) // accessing the bottom row
 
 A[R2][i]=B[x++];
 
 for(int i =R2-1;i>=R1+1;i--) // accessing the left column
 
 A[i][C1]=B[x++];
 
 
 void printArray() //Function for printing the array A
 
 for(int i=0;i<m;i++)
 
 for(int j=0;j<m;j++)
 
 System.out.print(A[i][j]+"t");
 
 System.out.println();
 
 
 public static void main(String args)
 
 SortBoundary ob = new SortBoundary();
 ob.input();
 System.out.println("*********************");
 System.out.println("The original matrix:");
 System.out.println("*********************");
 ob.printArray(); //Printing the original array
 ob.convert(); //Storing Boundary elements to a 1-D array
 ob.sortArray(); //Sorting the 1-D array (i.e. Boundary Elements)
 ob.fillSpiral(); //Storing the sorted Boundary elements back to original 2-D array
 System.out.println("*********************");
 System.out.println("The Rearranged matrix:");
 System.out.println("*********************");
 ob.printArray(); //Printing the rearranged array
 System.out.println("*********************");
 System.out.println("The sum of boundary elements is = "+sum); //Printing the sum of boundary elements
 
What I want to accomplish:
I want to fill the original array A with the sorted boundary elements B in a single loop at their respective positions in descending order.In my code I have used 4 different loops for accomplishing this task.Any suggestions or help to make my code better?
java array matrix
 
 
 1
 
 
 
 
 Cross-posted on Stack Overflow
 â Mast
 Feb 5 at 16:36
 
 
 
 
 
 
 
 
 
 Does your code currently work as expected? As in, does it do the job?
 â Mast
 Feb 5 at 16:37
 
 
 
 
 
 
 
 
 
 Yes.i want to improve it.
 â user159829
 Feb 5 at 16:38
 
 
 
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Question:
- Sort the boundary elements in descending order using any standard sorting technique and rearrange them in the matrix. 
- Calculate the sum of the boundary elements. 
- Display the original matrix, rearranged matrix and sum of the boundary elements. 
Code:
import java.util.*;
class SortBoundary
 int A, B, m, n; 
 static int sum=0;
 void input() //Function for taking all the necessary inputs
 m>10)
 
 System.out.println("Invalid Range");
 System.exit(0);
 
 else
 
 A = new int[m][m];
 n = m*m;
 B = new int[n]; // 1-D Array to store Boundary Elements
 System.out.println("Enter the elements of the Matrix : ");
 for(int i=0;i<m;i++)
 
 for(int j=0;j<m;j++)
 
 System.out.print("Enter a value : ");
 A[i][j]=sc.nextInt();
 
 
 
 
 /* The below function is used to store Boundary elements 
 * from array A to array B 
 */
 void convert()
 
 int x=0;
 for(int i=0;i<m;i++)
 
 for(int j=0;j<m;j++)
 i == m-1 
 
 
 void sortArray() //Function for sorting Boundary elements stored in array B
 
 int c = 0;
 for(int i=0; i<n-1; i++)
 
 for(int j=i+1; j<n; j++)
 
 if(B[i]<B[j]) // for ascending use B[i]>B[j]
 
 c = B[i];
 B[i] = B[j];
 B[j] = c;
 
 
 
 
 /* Function fillSpiral is filling the boundary of 2-D array in spiral
 * way from the elements of 1-D array
 */
 void fillSpiral()
 
 int R1=0, R2=m-1, C1=0, C2=m-1, x=0;
 for(int i=C1;i<=C2;i++) // accessing the top row
 
 A[R1][i]=B[x++];
 
 for(int i =R1+1;i<=R2;i++) // accessing the right column
 
 A[i][C2]=B[x++];
 
 for(int i =C2-1;i>=C1;i--) // accessing the bottom row
 
 A[R2][i]=B[x++];
 
 for(int i =R2-1;i>=R1+1;i--) // accessing the left column
 
 A[i][C1]=B[x++];
 
 
 void printArray() //Function for printing the array A
 
 for(int i=0;i<m;i++)
 
 for(int j=0;j<m;j++)
 
 System.out.print(A[i][j]+"t");
 
 System.out.println();
 
 
 public static void main(String args)
 
 SortBoundary ob = new SortBoundary();
 ob.input();
 System.out.println("*********************");
 System.out.println("The original matrix:");
 System.out.println("*********************");
 ob.printArray(); //Printing the original array
 ob.convert(); //Storing Boundary elements to a 1-D array
 ob.sortArray(); //Sorting the 1-D array (i.e. Boundary Elements)
 ob.fillSpiral(); //Storing the sorted Boundary elements back to original 2-D array
 System.out.println("*********************");
 System.out.println("The Rearranged matrix:");
 System.out.println("*********************");
 ob.printArray(); //Printing the rearranged array
 System.out.println("*********************");
 System.out.println("The sum of boundary elements is = "+sum); //Printing the sum of boundary elements
 
What I want to accomplish:
I want to fill the original array A with the sorted boundary elements B in a single loop at their respective positions in descending order.In my code I have used 4 different loops for accomplishing this task.Any suggestions or help to make my code better?
java array matrix
Question:
- Sort the boundary elements in descending order using any standard sorting technique and rearrange them in the matrix. 
- Calculate the sum of the boundary elements. 
- Display the original matrix, rearranged matrix and sum of the boundary elements. 
Code:
import java.util.*;
class SortBoundary
 int A, B, m, n; 
 static int sum=0;
 void input() //Function for taking all the necessary inputs
 m>10)
 
 System.out.println("Invalid Range");
 System.exit(0);
 
 else
 
 A = new int[m][m];
 n = m*m;
 B = new int[n]; // 1-D Array to store Boundary Elements
 System.out.println("Enter the elements of the Matrix : ");
 for(int i=0;i<m;i++)
 
 for(int j=0;j<m;j++)
 
 System.out.print("Enter a value : ");
 A[i][j]=sc.nextInt();
 
 
 
 
 /* The below function is used to store Boundary elements 
 * from array A to array B 
 */
 void convert()
 
 int x=0;
 for(int i=0;i<m;i++)
 
 for(int j=0;j<m;j++)
 i == m-1 
 
 
 void sortArray() //Function for sorting Boundary elements stored in array B
 
 int c = 0;
 for(int i=0; i<n-1; i++)
 
 for(int j=i+1; j<n; j++)
 
 if(B[i]<B[j]) // for ascending use B[i]>B[j]
 
 c = B[i];
 B[i] = B[j];
 B[j] = c;
 
 
 
 
 /* Function fillSpiral is filling the boundary of 2-D array in spiral
 * way from the elements of 1-D array
 */
 void fillSpiral()
 
 int R1=0, R2=m-1, C1=0, C2=m-1, x=0;
 for(int i=C1;i<=C2;i++) // accessing the top row
 
 A[R1][i]=B[x++];
 
 for(int i =R1+1;i<=R2;i++) // accessing the right column
 
 A[i][C2]=B[x++];
 
 for(int i =C2-1;i>=C1;i--) // accessing the bottom row
 
 A[R2][i]=B[x++];
 
 for(int i =R2-1;i>=R1+1;i--) // accessing the left column
 
 A[i][C1]=B[x++];
 
 
 void printArray() //Function for printing the array A
 
 for(int i=0;i<m;i++)
 
 for(int j=0;j<m;j++)
 
 System.out.print(A[i][j]+"t");
 
 System.out.println();
 
 
 public static void main(String args)
 
 SortBoundary ob = new SortBoundary();
 ob.input();
 System.out.println("*********************");
 System.out.println("The original matrix:");
 System.out.println("*********************");
 ob.printArray(); //Printing the original array
 ob.convert(); //Storing Boundary elements to a 1-D array
 ob.sortArray(); //Sorting the 1-D array (i.e. Boundary Elements)
 ob.fillSpiral(); //Storing the sorted Boundary elements back to original 2-D array
 System.out.println("*********************");
 System.out.println("The Rearranged matrix:");
 System.out.println("*********************");
 ob.printArray(); //Printing the rearranged array
 System.out.println("*********************");
 System.out.println("The sum of boundary elements is = "+sum); //Printing the sum of boundary elements
 
What I want to accomplish:
I want to fill the original array A with the sorted boundary elements B in a single loop at their respective positions in descending order.In my code I have used 4 different loops for accomplishing this task.Any suggestions or help to make my code better?
java array matrix
edited Feb 5 at 19:05


Billal BEGUERADJ
1
1
asked Feb 5 at 16:27
user159829
 
 
 1
 
 
 
 
 Cross-posted on Stack Overflow
 â Mast
 Feb 5 at 16:36
 
 
 
 
 
 
 
 
 
 Does your code currently work as expected? As in, does it do the job?
 â Mast
 Feb 5 at 16:37
 
 
 
 
 
 
 
 
 
 Yes.i want to improve it.
 â user159829
 Feb 5 at 16:38
 
 
 
add a comment |Â
 
 
 1
 
 
 
 
 Cross-posted on Stack Overflow
 â Mast
 Feb 5 at 16:36
 
 
 
 
 
 
 
 
 
 Does your code currently work as expected? As in, does it do the job?
 â Mast
 Feb 5 at 16:37
 
 
 
 
 
 
 
 
 
 Yes.i want to improve it.
 â user159829
 Feb 5 at 16:38
 
 
 
1
1
Cross-posted on Stack Overflow
â Mast
Feb 5 at 16:36
Cross-posted on Stack Overflow
â Mast
Feb 5 at 16:36
Does your code currently work as expected? As in, does it do the job?
â Mast
Feb 5 at 16:37
Does your code currently work as expected? As in, does it do the job?
â Mast
Feb 5 at 16:37
Yes.i want to improve it.
â user159829
Feb 5 at 16:38
Yes.i want to improve it.
â user159829
Feb 5 at 16:38
add a comment |Â
 1 Answer
 1
 
active
oldest
votes
up vote
1
down vote
accepted
Have no idea what's your question but here is a short method that's placing a boarder to a matrix. You could use the a as index for the B array.
private static void placeBoarders(int matrix, int n) 
 int a = 0;
 for (int i = 0; i < 4 * (n-1); i++) 
 switch (i/(n - 1)) 
 case 0:
 matrix[i%(n-1)][0] = a;
 break;
 case 1:
 matrix[n-1][i%(n-1)] = a;
 break;
 case 2:
 matrix[(n - 1) - i%(n-1)][n-1] = a;
 break;
 case 3:
 matrix[0][(n-1) - i%(n-1)] = a;
 break;
 default:
 throw new IndexOutOfBoundsException();
 
 a++;
 
The logic I've used here is that if I split the boarder to four equal chunks and I start from (top left -> bottom left -> bottom right -> top right -> top left) then I will have four pieces of size n-1. If you write it dawn on a paper then it will take like 5 mins to calculate the indexes.
For the opposite direction (top left -> top right -> bottom right -> bottom left -> top left) you should swap the matrix indexes just like this:
matrix[i%(n-1)][0] >> matrix[0][i%(n-1)]
matrix[n-1][i%(n-1)] >> matrix[i%(n-1)][n-1]
matrix[(n - 1) - i%(n-1)][n-1] >> matrix[n-1][(n - 1) - i%(n-1)]
matrix[0][(n-1) - i%(n-1)] >> matrix[(n-1) - i%(n-1)][0]
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 :
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;
 
 
In this case we do define
 
 
 
 
 
 
 My array A has the original array and Array B has the boundary elements of which I have found out the sum and also sorted it .Now my task is to fill the elements of array B in array A spirally.Why spirally you might ask? This is because I have to show the original array elements with the boundary elements .
 â user159829
 Feb 6 at 1:32
 
 
 
 
 
 
 
 
 
 If I get it right you have to fill the whole matrix not only the boarders ? is that correct :?
 â dbl
 Feb 6 at 8:31
 
 
 
 
 
 
 
 
 
 yes.Only the borders should be in descending order.For example consider the border matrix has [25,23,15,9,0,-2....] then 25 should be at (0,0) coordinates and like that the other elements should be arranged in a spiral order.
 â user159829
 Feb 6 at 8:47
 
 
 
 
 
 
 
 
 
 I will edit my answer but still not sure if I got your point correctly :)
 â dbl
 Feb 6 at 9:20
 
 
 
 
 
 
 
 
 
 Sorry I meant circular filling of boundaries in descending order not spiral.
 â user159829
 Feb 6 at 9:22
 
 
 
 |Â
show 5 more comments
 1 Answer
 1
 
active
oldest
votes
 1 Answer
 1
 
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Have no idea what's your question but here is a short method that's placing a boarder to a matrix. You could use the a as index for the B array.
private static void placeBoarders(int matrix, int n) 
 int a = 0;
 for (int i = 0; i < 4 * (n-1); i++) 
 switch (i/(n - 1)) 
 case 0:
 matrix[i%(n-1)][0] = a;
 break;
 case 1:
 matrix[n-1][i%(n-1)] = a;
 break;
 case 2:
 matrix[(n - 1) - i%(n-1)][n-1] = a;
 break;
 case 3:
 matrix[0][(n-1) - i%(n-1)] = a;
 break;
 default:
 throw new IndexOutOfBoundsException();
 
 a++;
 
The logic I've used here is that if I split the boarder to four equal chunks and I start from (top left -> bottom left -> bottom right -> top right -> top left) then I will have four pieces of size n-1. If you write it dawn on a paper then it will take like 5 mins to calculate the indexes.
For the opposite direction (top left -> top right -> bottom right -> bottom left -> top left) you should swap the matrix indexes just like this:
matrix[i%(n-1)][0] >> matrix[0][i%(n-1)]
matrix[n-1][i%(n-1)] >> matrix[i%(n-1)][n-1]
matrix[(n - 1) - i%(n-1)][n-1] >> matrix[n-1][(n - 1) - i%(n-1)]
matrix[0][(n-1) - i%(n-1)] >> matrix[(n-1) - i%(n-1)][0]
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 :
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;
 
 
In this case we do define
 
 
 
 
 
 
 My array A has the original array and Array B has the boundary elements of which I have found out the sum and also sorted it .Now my task is to fill the elements of array B in array A spirally.Why spirally you might ask? This is because I have to show the original array elements with the boundary elements .
 â user159829
 Feb 6 at 1:32
 
 
 
 
 
 
 
 
 
 If I get it right you have to fill the whole matrix not only the boarders ? is that correct :?
 â dbl
 Feb 6 at 8:31
 
 
 
 
 
 
 
 
 
 yes.Only the borders should be in descending order.For example consider the border matrix has [25,23,15,9,0,-2....] then 25 should be at (0,0) coordinates and like that the other elements should be arranged in a spiral order.
 â user159829
 Feb 6 at 8:47
 
 
 
 
 
 
 
 
 
 I will edit my answer but still not sure if I got your point correctly :)
 â dbl
 Feb 6 at 9:20
 
 
 
 
 
 
 
 
 
 Sorry I meant circular filling of boundaries in descending order not spiral.
 â user159829
 Feb 6 at 9:22
 
 
 
 |Â
show 5 more comments
up vote
1
down vote
accepted
Have no idea what's your question but here is a short method that's placing a boarder to a matrix. You could use the a as index for the B array.
private static void placeBoarders(int matrix, int n) 
 int a = 0;
 for (int i = 0; i < 4 * (n-1); i++) 
 switch (i/(n - 1)) 
 case 0:
 matrix[i%(n-1)][0] = a;
 break;
 case 1:
 matrix[n-1][i%(n-1)] = a;
 break;
 case 2:
 matrix[(n - 1) - i%(n-1)][n-1] = a;
 break;
 case 3:
 matrix[0][(n-1) - i%(n-1)] = a;
 break;
 default:
 throw new IndexOutOfBoundsException();
 
 a++;
 
The logic I've used here is that if I split the boarder to four equal chunks and I start from (top left -> bottom left -> bottom right -> top right -> top left) then I will have four pieces of size n-1. If you write it dawn on a paper then it will take like 5 mins to calculate the indexes.
For the opposite direction (top left -> top right -> bottom right -> bottom left -> top left) you should swap the matrix indexes just like this:
matrix[i%(n-1)][0] >> matrix[0][i%(n-1)]
matrix[n-1][i%(n-1)] >> matrix[i%(n-1)][n-1]
matrix[(n - 1) - i%(n-1)][n-1] >> matrix[n-1][(n - 1) - i%(n-1)]
matrix[0][(n-1) - i%(n-1)] >> matrix[(n-1) - i%(n-1)][0]
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 :
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;
 
 
In this case we do define
 
 
 
 
 
 
 My array A has the original array and Array B has the boundary elements of which I have found out the sum and also sorted it .Now my task is to fill the elements of array B in array A spirally.Why spirally you might ask? This is because I have to show the original array elements with the boundary elements .
 â user159829
 Feb 6 at 1:32
 
 
 
 
 
 
 
 
 
 If I get it right you have to fill the whole matrix not only the boarders ? is that correct :?
 â dbl
 Feb 6 at 8:31
 
 
 
 
 
 
 
 
 
 yes.Only the borders should be in descending order.For example consider the border matrix has [25,23,15,9,0,-2....] then 25 should be at (0,0) coordinates and like that the other elements should be arranged in a spiral order.
 â user159829
 Feb 6 at 8:47
 
 
 
 
 
 
 
 
 
 I will edit my answer but still not sure if I got your point correctly :)
 â dbl
 Feb 6 at 9:20
 
 
 
 
 
 
 
 
 
 Sorry I meant circular filling of boundaries in descending order not spiral.
 â user159829
 Feb 6 at 9:22
 
 
 
 |Â
show 5 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Have no idea what's your question but here is a short method that's placing a boarder to a matrix. You could use the a as index for the B array.
private static void placeBoarders(int matrix, int n) 
 int a = 0;
 for (int i = 0; i < 4 * (n-1); i++) 
 switch (i/(n - 1)) 
 case 0:
 matrix[i%(n-1)][0] = a;
 break;
 case 1:
 matrix[n-1][i%(n-1)] = a;
 break;
 case 2:
 matrix[(n - 1) - i%(n-1)][n-1] = a;
 break;
 case 3:
 matrix[0][(n-1) - i%(n-1)] = a;
 break;
 default:
 throw new IndexOutOfBoundsException();
 
 a++;
 
The logic I've used here is that if I split the boarder to four equal chunks and I start from (top left -> bottom left -> bottom right -> top right -> top left) then I will have four pieces of size n-1. If you write it dawn on a paper then it will take like 5 mins to calculate the indexes.
For the opposite direction (top left -> top right -> bottom right -> bottom left -> top left) you should swap the matrix indexes just like this:
matrix[i%(n-1)][0] >> matrix[0][i%(n-1)]
matrix[n-1][i%(n-1)] >> matrix[i%(n-1)][n-1]
matrix[(n - 1) - i%(n-1)][n-1] >> matrix[n-1][(n - 1) - i%(n-1)]
matrix[0][(n-1) - i%(n-1)] >> matrix[(n-1) - i%(n-1)][0]
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 :
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;
 
 
In this case we do define
Have no idea what's your question but here is a short method that's placing a boarder to a matrix. You could use the a as index for the B array.
private static void placeBoarders(int matrix, int n) 
 int a = 0;
 for (int i = 0; i < 4 * (n-1); i++) 
 switch (i/(n - 1)) 
 case 0:
 matrix[i%(n-1)][0] = a;
 break;
 case 1:
 matrix[n-1][i%(n-1)] = a;
 break;
 case 2:
 matrix[(n - 1) - i%(n-1)][n-1] = a;
 break;
 case 3:
 matrix[0][(n-1) - i%(n-1)] = a;
 break;
 default:
 throw new IndexOutOfBoundsException();
 
 a++;
 
The logic I've used here is that if I split the boarder to four equal chunks and I start from (top left -> bottom left -> bottom right -> top right -> top left) then I will have four pieces of size n-1. If you write it dawn on a paper then it will take like 5 mins to calculate the indexes.
For the opposite direction (top left -> top right -> bottom right -> bottom left -> top left) you should swap the matrix indexes just like this:
matrix[i%(n-1)][0] >> matrix[0][i%(n-1)]
matrix[n-1][i%(n-1)] >> matrix[i%(n-1)][n-1]
matrix[(n - 1) - i%(n-1)][n-1] >> matrix[n-1][(n - 1) - i%(n-1)]
matrix[0][(n-1) - i%(n-1)] >> matrix[(n-1) - i%(n-1)][0]
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 :
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;
 
 
In this case we do define
edited Feb 6 at 9:50
answered Feb 5 at 17:51
dbl
1413
1413
 
 
 
 
 
 
 My array A has the original array and Array B has the boundary elements of which I have found out the sum and also sorted it .Now my task is to fill the elements of array B in array A spirally.Why spirally you might ask? This is because I have to show the original array elements with the boundary elements .
 â user159829
 Feb 6 at 1:32
 
 
 
 
 
 
 
 
 
 If I get it right you have to fill the whole matrix not only the boarders ? is that correct :?
 â dbl
 Feb 6 at 8:31
 
 
 
 
 
 
 
 
 
 yes.Only the borders should be in descending order.For example consider the border matrix has [25,23,15,9,0,-2....] then 25 should be at (0,0) coordinates and like that the other elements should be arranged in a spiral order.
 â user159829
 Feb 6 at 8:47
 
 
 
 
 
 
 
 
 
 I will edit my answer but still not sure if I got your point correctly :)
 â dbl
 Feb 6 at 9:20
 
 
 
 
 
 
 
 
 
 Sorry I meant circular filling of boundaries in descending order not spiral.
 â user159829
 Feb 6 at 9:22
 
 
 
 |Â
show 5 more comments
 
 
 
 
 
 
 My array A has the original array and Array B has the boundary elements of which I have found out the sum and also sorted it .Now my task is to fill the elements of array B in array A spirally.Why spirally you might ask? This is because I have to show the original array elements with the boundary elements .
 â user159829
 Feb 6 at 1:32
 
 
 
 
 
 
 
 
 
 If I get it right you have to fill the whole matrix not only the boarders ? is that correct :?
 â dbl
 Feb 6 at 8:31
 
 
 
 
 
 
 
 
 
 yes.Only the borders should be in descending order.For example consider the border matrix has [25,23,15,9,0,-2....] then 25 should be at (0,0) coordinates and like that the other elements should be arranged in a spiral order.
 â user159829
 Feb 6 at 8:47
 
 
 
 
 
 
 
 
 
 I will edit my answer but still not sure if I got your point correctly :)
 â dbl
 Feb 6 at 9:20
 
 
 
 
 
 
 
 
 
 Sorry I meant circular filling of boundaries in descending order not spiral.
 â user159829
 Feb 6 at 9:22
 
 
 
My array A has the original array and Array B has the boundary elements of which I have found out the sum and also sorted it .Now my task is to fill the elements of array B in array A spirally.Why spirally you might ask? This is because I have to show the original array elements with the boundary elements .
â user159829
Feb 6 at 1:32
My array A has the original array and Array B has the boundary elements of which I have found out the sum and also sorted it .Now my task is to fill the elements of array B in array A spirally.Why spirally you might ask? This is because I have to show the original array elements with the boundary elements .
â user159829
Feb 6 at 1:32
If I get it right you have to fill the whole matrix not only the boarders ? is that correct :?
â dbl
Feb 6 at 8:31
If I get it right you have to fill the whole matrix not only the boarders ? is that correct :?
â dbl
Feb 6 at 8:31
yes.Only the borders should be in descending order.For example consider the border matrix has [25,23,15,9,0,-2....] then 25 should be at (0,0) coordinates and like that the other elements should be arranged in a spiral order.
â user159829
Feb 6 at 8:47
yes.Only the borders should be in descending order.For example consider the border matrix has [25,23,15,9,0,-2....] then 25 should be at (0,0) coordinates and like that the other elements should be arranged in a spiral order.
â user159829
Feb 6 at 8:47
I will edit my answer but still not sure if I got your point correctly :)
â dbl
Feb 6 at 9:20
I will edit my answer but still not sure if I got your point correctly :)
â dbl
Feb 6 at 9:20
Sorry I meant circular filling of boundaries in descending order not spiral.
â user159829
Feb 6 at 9:22
Sorry I meant circular filling of boundaries in descending order not spiral.
â user159829
Feb 6 at 9:22
 |Â
show 5 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186847%2fspiral-filling-an-existing-matrix-in-their-respective-positions-in-descending-or%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Cross-posted on Stack Overflow
â Mast
Feb 5 at 16:36
Does your code currently work as expected? As in, does it do the job?
â Mast
Feb 5 at 16:37
Yes.i want to improve it.
â user159829
Feb 5 at 16:38