Refactor files to be in correctly nested packages (#6120)

This commit is contained in:
varada610
2025-01-10 23:17:40 -08:00
committed by GitHub
parent a9633c0000
commit 1e6ed97fcf
13 changed files with 75 additions and 77 deletions

View File

@@ -1,62 +0,0 @@
package com.thealgorithms.others;
import java.util.ArrayList;
import java.util.List;
public class PrintAMatrixInSpiralOrder {
/**
* Search a key in row and column wise sorted matrix
*
* @param matrix matrix to be searched
* @param row number of rows matrix has
* @param col number of columns matrix has
* @author Sadiul Hakim : https://github.com/sadiul-hakim
*/
public List<Integer> print(int[][] matrix, int row, int col) {
// r traverses matrix row wise from first
int r = 0;
// c traverses matrix column wise from first
int c = 0;
int i;
List<Integer> result = new ArrayList<>();
while (r < row && c < col) {
// print first row of matrix
for (i = c; i < col; i++) {
result.add(matrix[r][i]);
}
// increase r by one because first row printed
r++;
// print last column
for (i = r; i < row; i++) {
result.add(matrix[i][col - 1]);
}
// decrease col by one because last column has been printed
col--;
// print rows from last except printed elements
if (r < row) {
for (i = col - 1; i >= c; i--) {
result.add(matrix[row - 1][i]);
}
row--;
}
// print columns from first except printed elements
if (c < col) {
for (i = row - 1; i >= r; i--) {
result.add(matrix[i][c]);
}
c++;
}
}
return result;
}
}

View File

@@ -1,73 +0,0 @@
package com.thealgorithms.others;
import java.util.Scanner;
/**
* Given a matrix of size n x n We have to rotate this matrix by 90 Degree Here
* is the algorithm for this problem .
*/
final class RotateMatrixBy90Degrees {
private RotateMatrixBy90Degrees() {
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[][] arr = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
Rotate.rotate(arr);
printMatrix(arr);
}
sc.close();
}
static void printMatrix(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println("");
}
}
}
/**
* Class containing the algo to roate matrix by 90 degree
*/
final class Rotate {
private Rotate() {
}
static void rotate(int[][] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i > j) {
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
}
int i = 0;
int k = n - 1;
while (i < k) {
for (int j = 0; j < n; j++) {
int temp = a[i][j];
a[i][j] = a[k][j];
a[k][j] = temp;
}
i++;
k--;
}
}
}