-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Spiral and SwapElements class into arrays package.
- Loading branch information
1 parent
9821be7
commit 6ccb22b
Showing
3 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |