Skip to content

Commit c0c84bc

Browse files
authored
Merge pull request dubesar#4 from dubesar/master
update
2 parents 1fdb620 + ddb6ff1 commit c0c84bc

File tree

552 files changed

+17142
-154
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

552 files changed

+17142
-154
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Algorithms/Fibonacci.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Fibonacci series in Java
3+
* Program that prints the first N numbers of the Fibonacci series
4+
* The first number in the series is 1, the second number is 1, and each of the
5+
* following is the sum of the previous two
6+
*
7+
* @author IryaDev
8+
*/
9+
10+
import java.util.*;
11+
public class Fibonacci{
12+
13+
public static void main(String[] args){
14+
Scanner sc = new Scanner(System.in);
15+
long number,fib_1 = 1,fib_2 = 1,i;
16+
17+
do{
18+
System.out.print("Enter a number greater than one: ");
19+
number = sc.nextInt();
20+
}while(number<=1);
21+
System.out.println("The first " + number + " terms of the Fibonacci series are:");
22+
System.out.print(fib_1 + " ");
23+
24+
for(i=2;i<=number;i++){
25+
System.out.print(fib_2 + " ");
26+
fib_2 = fib_1 + fib_2;
27+
fib_1 = fib_2 - fib_1;
28+
}
29+
System.out.println();
30+
}
31+
}

Algorithms/FloydWarshall.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Arrays;
2+
3+
public class FloydWarshall {
4+
static final int INF = 10000;
5+
6+
public static int[][] solve(int graph[][]) {
7+
int result[][] = new int[graph.length][graph.length];
8+
9+
for (int i = 0; i < graph.length; i++)
10+
for (int j = 0; j < graph.length; j++)
11+
result[i][j] = graph[i][j];
12+
13+
for (int k = 0; k < graph.length; k++)
14+
{
15+
for (int i = 0; i < graph.length; i++)
16+
{
17+
for (int j = 0; j < graph.length; j++)
18+
{
19+
result[i][j] = Math.min(result[i][j], result[i][k] + result[k][j]);
20+
}
21+
}
22+
}
23+
24+
return result;
25+
}
26+
27+
public static void main(String args[])
28+
{
29+
int graph[][] =
30+
{{0, 3, INF, INF, 2},
31+
{INF, 0, 3, INF, 4},
32+
{1, INF, 0, 1, 3},
33+
{INF, 2, INF, 0, INF},
34+
{4, 5, INF, INF, 0}};
35+
36+
int result[][] = solve(graph);
37+
System.out.println(Arrays.deepToString(result));
38+
}
39+
}

Algorithms/GaussJordanAlgo.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import java.util.Scanner;
2+
3+
public class mainclass {
4+
//got this convert to fraction from stack overflow
5+
static private String convertDecimalToFraction(double x){
6+
if (x < 0){
7+
return "-" + convertDecimalToFraction(-x);
8+
}
9+
double tolerance = 1.0E-6;
10+
double h1=1; double h2=0;
11+
double k1=0; double k2=1;
12+
double b = x;
13+
do {
14+
double a = Math.floor(b);
15+
double aux = h1; h1 = a*h1+h2; h2 = aux;
16+
aux = k1; k1 = a*k1+k2; k2 = aux;
17+
b = 1/(b-a);
18+
} while (Math.abs(x-h1/k1) > x*tolerance);
19+
if (k1==1)
20+
return ""+h1;
21+
else
22+
return h1+"/"+k1;
23+
}
24+
private static void displayMat(float[][] matrixdisp)
25+
{
26+
for (int i=0;i<3;i++)
27+
{
28+
for (int j=0;j<4;j++)
29+
{
30+
System.out.print(convertDecimalToFraction(matrixdisp[i][j])+ " ");
31+
32+
}
33+
System.out.println();
34+
}
35+
System.out.println("***********************************************************");
36+
}
37+
private static void pivot(float[][] piviotmat, int x, int y)
38+
{
39+
float value= piviotmat[x][y];
40+
float z=1/value;// value to piviot
41+
System.out.println("piviting row "+ x+ "by multiplying "+convertDecimalToFraction(z));
42+
for (int k=0;k<4;k++) {
43+
44+
piviotmat[x][k] = piviotmat[x][k] * z;//here we multiply the pivit
45+
}
46+
displayMat(piviotmat);
47+
float multiplier;
48+
for (int l=0;l<3;l++)
49+
{
50+
if (l!=y) {
51+
multiplier = piviotmat[l][y];//value to multiply to piviot
52+
System.out.println("multiplying row "+ x+ "by "+convertDecimalToFraction(multiplier));
53+
for (int k = 0; k < 4; k++) {
54+
piviotmat[x][k] = piviotmat[x][k] * multiplier;//multiply pivit with row multiplier
55+
}
56+
displayMat(piviotmat);
57+
System.out.println("substracting row "+ l+ "from row "+x);
58+
for (int k = 0; k < 4; k++) {
59+
piviotmat[l][k] = piviotmat[l][k] - piviotmat[x][k];
60+
61+
}
62+
displayMat(piviotmat);
63+
System.out.println("restoring pivit row "+ x+ "by dividing "+convertDecimalToFraction(multiplier));
64+
for (int k = 0; k < 4; k++) {
65+
piviotmat[x][k] = piviotmat[x][k] /multiplier;//restore the multipied pivit matrix
66+
}
67+
displayMat(piviotmat);
68+
}
69+
70+
}
71+
72+
}
73+
public static void main(String[] args) {
74+
float[][] matrix;
75+
matrix = new float[3][4];
76+
for (int i=0;i<3;i++)
77+
{
78+
for (int j=0;j<4;j++)
79+
{
80+
System.out.println("enter the values : ");
81+
Scanner sc= new Scanner(System.in);
82+
float value= sc.nextFloat();
83+
matrix[i][j]=value;// checking matrix
84+
85+
}
86+
}
87+
displayMat(matrix);
88+
for (int i=0;i<3;i++)
89+
{
90+
for (int j=0;j<4;j++)
91+
{
92+
if (i==j&&matrix[i][j]!=1)
93+
{
94+
pivot(matrix, i, j);
95+
96+
}
97+
}
98+
}
99+
for (int m=0;m<3;m++)
100+
{
101+
switch (m)
102+
{
103+
case 0: System.out.println("value of x is "+ convertDecimalToFraction(matrix[0][3]));
104+
break;
105+
case 1: System.out.println("value of y is "+ convertDecimalToFraction(matrix[1][3]));
106+
break;
107+
case 2: System.out.println("value of z is "+ convertDecimalToFraction(matrix[2][3]));
108+
break;
109+
}
110+
}
111+
}
112+
}
113+
114+

Algorithms/Jump_Search.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public static int jumpSearch(int[] arrayToSearch, int element) {
2+
int blockSize = (int) Math.floor(Math.sqrt(arrayToSearch.length));
3+
4+
int currentLastIndex = blockSize-1;
5+
6+
// Jump to next block as long as target element is > currentLastIndex
7+
// and the array end has not been reached
8+
while (currentLastIndex < arrayToSearch.length && element > arrayToSearch[currentLastIndex]) {
9+
currentLastIndex += blockSize;
10+
}
11+
12+
// Find accurate position of target element using Linear Search
13+
for (int currentSearchIndex = currentLastIndex - blockSize + 1;
14+
currentSearchIndex <= currentLastIndex && currentSearchIndex < arrayToSearch.length; currentSearchIndex++) {
15+
if (element == arrayToSearch[currentSearchIndex]) {
16+
return currentSearchIndex;
17+
}
18+
}
19+
// Target element not found. Return negative integer as element position.
20+
return -1;
21+
}

Algorithms/Strassen Algorithm.java

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import java.util.Scanner;
2+
3+
4+
public class Strassen
5+
{
6+
// Function to multiply matrices
7+
public int[][] multiply(int[][] A, int[][] B)
8+
{
9+
int n = A.length;
10+
int[][] R = new int[n][n];
11+
12+
/** base case **/
13+
if (n == 1)
14+
R[0][0] = A[0][0] * B[0][0];
15+
else
16+
{
17+
int[][] A11 = new int[n/2][n/2];
18+
int[][] A12 = new int[n/2][n/2];
19+
int[][] A21 = new int[n/2][n/2];
20+
int[][] A22 = new int[n/2][n/2];
21+
int[][] B11 = new int[n/2][n/2];
22+
int[][] B12 = new int[n/2][n/2];
23+
int[][] B21 = new int[n/2][n/2];
24+
int[][] B22 = new int[n/2][n/2];
25+
26+
/** Dividing matrix A into 4 halves **/
27+
split(A, A11, 0 , 0);
28+
split(A, A12, 0 , n/2);
29+
split(A, A21, n/2, 0);
30+
split(A, A22, n/2, n/2);
31+
32+
33+
/** Dividing matrix B into 4 halves **/
34+
split(B, B11, 0 , 0);
35+
split(B, B12, 0 , n/2);
36+
split(B, B21, n/2, 0);
37+
split(B, B22, n/2, n/2);
38+
39+
/**
40+
M1 = (A11 + A22)(B11 + B22)
41+
M2 = (A21 + A22) B11
42+
M3 = A11 (B12 - B22)
43+
M4 = A22 (B21 - B11)
44+
M5 = (A11 + A12) B22
45+
M6 = (A21 - A11) (B11 + B12)
46+
M7 = (A12 - A22) (B21 + B22)
47+
**/
48+
49+
int [][] M1 = multiply(add(A11, A22), add(B11, B22));
50+
int [][] M2 = multiply(add(A21, A22), B11);
51+
int [][] M3 = multiply(A11, sub(B12, B22));
52+
int [][] M4 = multiply(A22, sub(B21, B11));
53+
int [][] M5 = multiply(add(A11, A12), B22);
54+
int [][] M6 = multiply(sub(A21, A11), add(B11, B12));
55+
int [][] M7 = multiply(sub(A12, A22), add(B21, B22));
56+
57+
/**
58+
C11 = M1 + M4 - M5 + M7
59+
C12 = M3 + M5
60+
C21 = M2 + M4
61+
C22 = M1 - M2 + M3 + M6
62+
**/
63+
64+
int [][] C11 = add(sub(add(M1, M4), M5), M7);
65+
int [][] C12 = add(M3, M5);
66+
int [][] C21 = add(M2, M4);
67+
int [][] C22 = add(sub(add(M1, M3), M2), M6);
68+
69+
/** Joining 4 halves into one result matrix **/
70+
join(C11, R, 0 , 0);
71+
join(C12, R, 0 , n/2);
72+
join(C21, R, n/2, 0);
73+
join(C22, R, n/2, n/2);
74+
}
75+
76+
/** Returning the resultant matrix **/
77+
return R;
78+
}
79+
80+
/** Funtion to add two matrices **/
81+
public int[][] add(int[][] A, int[][] B)
82+
{
83+
int n = A.length;
84+
int[][] C = new int[n][n];
85+
for (int i = 0; i < n; i++)
86+
for (int j = 0; j < n; j++)
87+
C[i][j] = A[i][j] + B[i][j];
88+
return C;
89+
}
90+
91+
/** Funtion to subtract two matrices **/
92+
public int[][] sub(int[][] A, int[][] B)
93+
{
94+
int n = A.length;
95+
int[][] C = new int[n][n];
96+
for (int i = 0; i < n; i++)
97+
for (int j = 0; j < n; j++)
98+
C[i][j] = A[i][j] - B[i][j];
99+
return C;
100+
}
101+
102+
/** Funtion to split parent matrix into child matrices **/
103+
public void split(int[][] P, int[][] C, int iB, int jB)
104+
{
105+
for(int i1 = 0, i2 = iB; i1 < C.length; i1++, i2++)
106+
for(int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++)
107+
C[i1][j1] = P[i2][j2];
108+
}
109+
110+
/** Funtion to join child matrices intp parent matrix **/
111+
public void join(int[][] C, int[][] P, int iB, int jB)
112+
{
113+
for(int i1 = 0, i2 = iB; i1 < C.length; i1++, i2++)
114+
for(int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++)
115+
P[i2][j2] = C[i1][j1];
116+
}
117+
118+
/** Main function **/
119+
public static void main (String[] args)
120+
{
121+
Scanner sc = new Scanner(System.in);
122+
123+
/** Making an object of Strassen class **/
124+
Strassen s = new Strassen();
125+
126+
System.out.println("Enter the order of matrices (n) :");
127+
int N = sc.nextInt();
128+
129+
/** Accepting two 2d matrices **/
130+
System.out.println("Enter N*N order matrix 1\n");
131+
int[][] A = new int[N][N];
132+
for (int i = 0; i < N; i++)
133+
for (int j = 0; j < N; j++)
134+
A[i][j] = sc.nextInt();
135+
136+
System.out.println("Enter N*N order matrix 2\n");
137+
int[][] B = new int[N][N];
138+
for (int i = 0; i < N; i++)
139+
for (int j = 0; j < N; j++)
140+
B[i][j] = sc.nextInt();
141+
142+
int[][] C = s.multiply(A, B);
143+
144+
System.out.println("\nProduct of matrices 1 and 2 is : ");
145+
for (int i = 0; i < N; i++)
146+
{
147+
for (int j = 0; j < N; j++)
148+
System.out.print(C[i][j] +" ");
149+
System.out.println();
150+
}
151+
152+
}
153+
}

0 commit comments

Comments
 (0)