Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aaa #573

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Aaa #573

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions java/matrix/Identity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
public class IdentityMatrix
{
public static void main(String[] args) {
int rows, cols;
boolean flag = true;

//Initialize matrix a
int a[][] = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
};

//Calculates the number of rows and columns present in the given matrix

rows = a.length;
cols = a[0].length;

//Checks whether given matrix is a square matrix or not
if(rows != cols){
System.out.println("Matrix should be a square matrix");
}
else {
//Checks if diagonal elements are equal to 1 and rest of elements are 0
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(i == j && a[i][j] != 1){
flag = false;
break;
}
if(i != j && a[i][j] != 0){
flag = false;
break;
}
}
}

if(flag)
System.out.println("Given matrix is an identity matrix");
else
System.out.println("Given matrix is not an identity matrix");
}
}
}
18 changes: 18 additions & 0 deletions java/matrix/MatrixAddition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class MatrixAdditionExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};

//creating another matrix to store the sum of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns

//adding and printing addition of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j]; //use - for subtraction
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}}
18 changes: 18 additions & 0 deletions java/matrix/matrixSub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class MatrixSubExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};

//creating another matrix to store the difference of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns

//Subtracting and printing addition of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]-b[i][j]; //use - for subtraction
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}}
102 changes: 102 additions & 0 deletions java/matrix/spiral.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
public class SpiralPatternExample1
{
//defining method to print the spiral pattern or matrix
static void printSpiralPattern(int size)
{
//create two variables row and col to traverse rows and columns
int row = 0, col = 0;
int boundary = size - 1;
int sizeLeft = size - 1;
int flag = 1;
//variables r, l, u and d are used to determine the movement
// r = right, l = left, d = down, u = upper
char move = 'r';
//creating a 2D array for matrix
int[][] matrix =new int [size][size];
for (int i = 1; i < size * size + 1; i++)
{
//assigning values
matrix[row][col] = i;
//switch-case to determine the next index
switch (move)
{
//if right, go right
case 'r':
col += 1;
break;
//if left, go left
case 'l':
col -= 1;
break;
//if up, go up
case 'u':
row -= 1;
break;
//if down, go down
case 'd':
row += 1;
break;
}
//checks if the matrix has reached the array boundary
if (i == boundary)
{
//adds the left size for the next boundary
boundary = boundary + sizeLeft;
//decrease the size left by 1, if 2 rotations have been made
if (flag != 2)
{
flag = 2;
}
else
{
flag = 1;
sizeLeft -= 1;
}
//switch-case to rotate the movement
switch (move)
{
//if right, rotate to down
case 'r':
move = 'd';
break;
// if down, rotate to left
case 'd':
move = 'l';
break;
// if left, rotate to up
case 'l':
move = 'u';
break;
// if up, rotate to right
case 'u':
move = 'r';
break;
}
}
}
//printing the spiral matrix or pattern
//outer for loop for rows
for (row = 0; row < size; row++)
{
//inner for loop for columns
for (col = 0; col < size; col++)
{
int n = matrix[row][col];
if(n < 10)
System.out.print(n +" ");
else
System.out.print(n +" ");
}
System.out.println();
}
}
//driver Code
public static void main(String args[])
{
//size of the array?s row and column
int size = 5;
System.out.println("Spiral Matrix or Pattern is: \n");
//calling the method that prints the spiral pattern or matrix
printSpiralPattern(size);
}
}
32 changes: 32 additions & 0 deletions java/matrix/spiral2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.Scanner;
import java.lang.Math;
public class SpiralPatternExample2
{
//function to print the spiral pattern
public static void printPattern(int n)
{
//detrmines the boundary size of the array
int size = 2 * n - 1;
//inner loop
for(int i = 1; i <= size; i++)
{
//outer loop
for(int j = 1; j <= size; j++)
{
//calculates and prints the values for pattern
System.out.print(Math.max(Math.abs(i - n), Math.abs(j - n)) + 1 + " ");
}
System.out.println();
}
}
//driver code
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
System.out.println();
//function calling
printPattern(n);
}
}
36 changes: 36 additions & 0 deletions java/sorting/matrixSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.*;

public class Main {
public static void main(String[] args)
{
// Initialize the 2D vector with some values
List<List<Integer> > v
= new ArrayList<>(Arrays.asList(
new ArrayList<>(Arrays.asList(5, 4, 7)),
new ArrayList<>(Arrays.asList(1, 3, 8)),
new ArrayList<>(Arrays.asList(2, 9, 6))));

int n = v.size();
List<Integer> x = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
x.add(v.get(i).get(j));
}
}
Collections.sort(x);
int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
v.get(i).set(j, x.get(k++));
}
}

System.out.println("Sorted Matrix Will be:");
for (List<Integer> row : v) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}