Skip to content

Commit

Permalink
Add Spiral and SwapElements class into arrays package.
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianUrbaniak committed Oct 19, 2022
1 parent 9821be7 commit 6ccb22b
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions arrays/Spiral.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package arrays;

//it returns a two-dimensional array representing a table with sizes of the given parameters
//a table contains numbers from 1 up to number received by multiplying number of rows and columns
//numbers fill the table clockwise from the top-level corner in a spiral manner

public class Spiral {
static int[][] spiral(int rows, int columns) {
int row = 0;
int col = 0;

int boundary = columns - 1;
int verticalBoundary = rows - 1;
int horizontalBoundary = columns - 1;

//determine movement r = right...
char move = 'r';

// switching between horizontal and vertical rotations
char shift = 'v';

//Creating array
int[][] matrix = new int[rows][columns];

// One column case
if(columns == 1) {
for (int i = 1; i < rows * columns + 1; i++) {
matrix[row][col] = i;
row++;
}
return matrix;
}

for (int i = 1; i < rows * columns + 1; i++) {
matrix [row][col] = i;

//determining the cell's next index
switch (move) {
// go right
case 'r':
col += 1;
break;
// go left
case 'l':
col -= 1;
break;
// go up
case 'u':
row -= 1;
break;
// go down
case 'd':
row += 1;
break;
}
//check if a boundary has been reached
if(boundary == i){
switch (shift) {
case 'v':
if (boundary == i){
boundary += verticalBoundary;
verticalBoundary -= 1;
shift = 'h';
}
case 'h':
if (boundary == i){
boundary += horizontalBoundary;
horizontalBoundary -= 1;
shift = 'v';
}
}
//change direction
switch (move) {
// go right
case 'r':
move = 'd';
break;
// go left
case 'l':
move = 'u';
break;
// go up
case 'u':
move = 'r';
break;
// go down
case 'd':
move = 'l';
break;
}
}
}
return matrix;
}
}
30 changes: 30 additions & 0 deletions arrays/SwapElements.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package arrays;

public class SwapElements {

// it shifts all the elements in given array to the right by 1 position
static void cycleSwap(int[] array) {
if(array.length == 0){
return;
}
int temp = array[array.length - 1];
for(int i = array.length - 2; i > -1; i--){
array[i + 1] = array[i];
}
array[0] = temp;
}

// it shifts all the elements in the given array to the right direction by specified numbers of postion
static void cycleSwap(int[] array, int shift) {
if(array.length == 0){
return;
}
for(int j = 1; j <= shift; j++){
int temp = array[array.length - 1];
for(int i = array.length - 2; i > -1; i--){
array[i + 1] = array[i];
}
array[0] = temp;
}
}
}

0 comments on commit 6ccb22b

Please sign in to comment.