Skip to content

Commit e5ee749

Browse files
committed
Transformed Java/Algorithms folder from submodule to regular folder
1 parent ca9529b commit e5ee749

File tree

10 files changed

+402
-1
lines changed

10 files changed

+402
-1
lines changed

Java/Algorithms

Lines changed: 0 additions & 1 deletion
This file was deleted.

Java/Algorithms/.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"java.project.sourcePaths": ["src"],
3+
"java.project.outputPath": "bin",
4+
"java.project.referencedLibraries": [
5+
"lib/**/*.jar"
6+
]
7+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package matrixAlgebra;
2+
3+
import java.util.Arrays;
4+
5+
6+
public class SquareMatrixMultiplication {
7+
8+
public static int[][] squareMatrixMultiply(int[][] A, int[][] B) {
9+
10+
int n = A.length; // n equals the number of rows in A
11+
int[][] c = new int[n][n];
12+
13+
for(int i = 0; i < n; i++) {
14+
for(int j = 0; j < n; j++) {
15+
for(int k = 0; k < n; k++) {
16+
c[i][j] += A[i][k] * B[k][j];
17+
}
18+
}
19+
}
20+
return c;
21+
}
22+
23+
public static void main(String[] args) {
24+
int[][] A, B, C;
25+
26+
A = new int[][] {
27+
{2, 3},
28+
{4, 6}
29+
};
30+
31+
B = new int[][] {
32+
{11, 9},
33+
{2, 16}
34+
};
35+
36+
C = squareMatrixMultiply(A, B);
37+
for(int[] c: C) {
38+
System.out.println(Arrays.toString(c));
39+
}
40+
}
41+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package matrixAlgebra;
2+
3+
import java.util.ArrayList;
4+
5+
6+
public class SquareMatrixMultiplicationRecursive {
7+
8+
public static int[][] squareMatrixMultiplication(int[][] A, int[][] B) {
9+
ArrayList<ArrayList<Integer>> A1, B2;
10+
11+
int rows = A.length, cols = A[0].length;
12+
for(int i = 0; i < rows; i++) {
13+
for(int j = 0; j < cols; j++) {
14+
try {
15+
16+
/* */
17+
18+
} catch(IndexOutOfBoundsException e) {
19+
20+
/* */
21+
22+
}
23+
}
24+
}
25+
26+
return null;
27+
}
28+
29+
/* Note: int i is just to stop the compiler from complaining; redundant parameter*/
30+
public static ArrayList<ArrayList<Integer>> squareMatrixMultiplication(int[][] A, int[][] B, int i) {
31+
int n = A.length;
32+
33+
if (n == 1) {
34+
35+
} else {
36+
37+
/* Calculate the start, mid, and end indexes */
38+
int start = 0, end = n - 1;
39+
int mid = (int) Math.floor((start + end) / 2);
40+
41+
/* Partition A, B, and C into submatrices */
42+
int[][] A11 = subarray(A, start, mid, start, mid);
43+
int[][] A12 = subarray(A, start, mid, mid + 1, end);
44+
int[][] A21 = subarray(A, mid + 1, end, start, end);
45+
int[][] A22 = subarray(A, mid + 1, end, mid + 1, end);
46+
47+
int[][] B11 = subarray(A, start, mid, start, mid);
48+
int[][] B12 = subarray(A, start, mid, mid + 1, end);
49+
int[][] B21 = subarray(A, mid + 1, end, start, end);
50+
int[][] B22 = subarray(A, mid + 1, end, mid + 1, end);
51+
52+
}
53+
54+
return null;
55+
}
56+
57+
public static int[][] subarray(int[][] nums, int rowStart, int rowEnd, int colStart, int colEnd) {
58+
int n = nums.length;
59+
int[][] sub = new int[n][n];
60+
for(int i = rowStart; i <= rowEnd; i++) {
61+
for(int j = colStart; j <= colEnd; j++) {
62+
sub[i - rowStart][j - colStart] = nums[i][j];
63+
}
64+
}
65+
66+
return sub;
67+
}
68+
69+
public static int[][] sum(int[][] A, int[][] B) {
70+
int n = A.length;
71+
int[][] sum = new int[n][n];
72+
73+
for(int i = 0; i < n; i++) {
74+
for(int j = 0; j < n; j++) {
75+
sum[i][j] = A[i][j] + B[i][j];
76+
}
77+
}
78+
79+
return sum;
80+
}
81+
82+
public static void main(String[] args) {
83+
84+
}
85+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package searchAlgorithms;
2+
3+
4+
public class BinarySearch {
5+
public static int binarySearch(Double[] data, Double value) {
6+
int low = 0;
7+
int high = data.length - 1;
8+
int mid = (int) (low + high) / 2;
9+
10+
while (low <= high) {
11+
if (data[mid].equals(value)) {
12+
return mid;
13+
} else {
14+
if (data[mid].compareTo(value) > 0) {
15+
high = mid - 1;
16+
mid = (int) (low + high) / 2;
17+
} else if(data[mid].compareTo(value) < 0) {
18+
low = mid + 1;
19+
mid = (int) (low + high) / 2;
20+
}
21+
return 0;
22+
}
23+
}
24+
return -1;
25+
}
26+
27+
public static void main(String[] args) {
28+
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package searchAlgorithms;
2+
3+
import java.util.Random;
4+
import java.util.Arrays;
5+
6+
public class LinearSearch {
7+
8+
public static int linearSearch(Object[] data, Object value) {
9+
for (int i = 0; i < data.length; i++) {
10+
if (data[i].equals(value)) return i;
11+
}
12+
13+
return -1;
14+
}
15+
16+
public static void main(String[] args) {
17+
Random random = new Random();
18+
Double[] data = new Double[10];
19+
for (int i = 0; i < 10; i++) {
20+
data[i] = random.nextDouble() * 100;
21+
}
22+
23+
double val1 = data[5];
24+
double val2 = 34.4f;
25+
26+
System.out.println("Data: " + Arrays.toString(data));
27+
28+
System.out.printf("val1: %f -> result: %d\n", val1, linearSearch(data, val1));
29+
System.out.printf("val2: %f -> result: %d\n", val2, linearSearch(data, val2));
30+
31+
}
32+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package searchAlgorithms;
2+
3+
4+
public class MaximumSubarrayProblem {
5+
6+
public static double[] findMaxCrossingSubarray(double[] A, int low, int mid, int high) {
7+
double leftSum = Double.NEGATIVE_INFINITY;
8+
double sum = 0;
9+
int maxLeft = mid;
10+
for (int i = mid; i >= low; i--) {
11+
sum = sum + A[i];
12+
if (sum > leftSum) {
13+
leftSum = sum;
14+
maxLeft = i;
15+
}
16+
}
17+
18+
double rightSum = Double.NEGATIVE_INFINITY;
19+
sum = 0;
20+
int maxRight = mid + 1;
21+
for (int j = mid + 1; j < high; j++) {
22+
sum = sum + A[j];
23+
if (sum > rightSum) {
24+
rightSum = sum;
25+
maxRight = j;
26+
}
27+
}
28+
return new double[]{maxLeft, maxRight, leftSum + rightSum};
29+
}
30+
31+
public static double[] findMaximumSubarray(double[] A, int low, int high) {
32+
if (high == low) {
33+
return new double[] {low, high, A[low]};
34+
}
35+
36+
int mid = (int) Math.floor((low + high) / 2);
37+
double[] left = findMaximumSubarray(A, low, mid);
38+
double[] right = findMaximumSubarray(A, mid + 1, high);
39+
double[] cross = findMaxCrossingSubarray(A, low, mid, high);
40+
41+
double leftSum = left[2], rightSum = right[2], crossSum = cross[2];
42+
if (leftSum >= rightSum && leftSum >= crossSum) {
43+
return left;
44+
}
45+
else if (rightSum >= leftSum && rightSum >= crossSum) {
46+
return right;
47+
}
48+
return cross;
49+
}
50+
51+
public static void main(String[] args) {
52+
double[] prices = new double[] {13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, - 5, -22, 15,-4, 7};
53+
double[] sub = findMaximumSubarray(prices, 0, prices.length - 1);
54+
55+
int low = (int) sub[0], high = (int) sub[1];
56+
double maxValue = sub[2];
57+
58+
System.out.println("Max Subarray: ");
59+
for(int i = low; i <= high; i++) {
60+
System.out.print(prices[i] + " ");
61+
}
62+
System.out.println("\nMax value: " + maxValue);
63+
}
64+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package sortAlgorithms;
2+
3+
import java.util.Arrays;
4+
5+
6+
public class InsertionSort {
7+
8+
public static void insertionSort(int[] nums) {
9+
int key;
10+
int j;
11+
12+
for(int i = 1; i < nums.length - 1; i++) {
13+
key = nums[i];
14+
j = i - 1;
15+
16+
while (j >= 0 && nums[j] > key) {
17+
nums[j + 1] = nums[j];
18+
nums[j] = key;
19+
j -= 1;
20+
}
21+
}
22+
}
23+
24+
public static void main(String[] args) {
25+
int[] nums = new int[] {11, 5, 3, 2, 5, 7, 8, 9, 42};
26+
System.out.println("Original: " + Arrays.toString(nums));
27+
28+
insertionSort(nums);
29+
30+
System.out.println("Sorted: " + Arrays.toString(nums));
31+
}
32+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package sortAlgorithms;
2+
3+
import java.util.Arrays;
4+
5+
6+
public class MergeSort {
7+
public static void mergerSort(int[] nums) {
8+
if (nums.length <= 1) {
9+
/* Do nothing */
10+
return;
11+
12+
} else {
13+
mergeSort(nums, 0, nums.length - 1);
14+
}
15+
}
16+
17+
public static void mergeSort(int[] nums, int leftBound, int rightBound) {
18+
if (leftBound < rightBound) {
19+
int mid = (int) Math.floor((leftBound + rightBound) / 2);
20+
mergeSort(nums, leftBound, mid);
21+
mergeSort(nums, mid + 1, rightBound);
22+
mergeSortAux(nums, leftBound, mid, rightBound);
23+
}
24+
}
25+
26+
public static void mergeSortAux(int[] nums, int leftBound, int mid, int rightBound) {
27+
int n1 = mid - leftBound + 1;
28+
int n2 = rightBound - mid;
29+
30+
int[] L = new int[n1 + 1], R = new int[n2 + 1];
31+
32+
for (int i = leftBound; i <= n1; i++) {
33+
L[i] = nums[i];
34+
}
35+
L[n1] = (int) Double.POSITIVE_INFINITY;
36+
37+
for (int j = mid + 1; j <= rightBound; j++) {
38+
R[j - (mid + 1)] = nums[j];
39+
}
40+
R[n2] = (int) Double.POSITIVE_INFINITY;
41+
42+
int i = 0, j = 0;
43+
for (int k = leftBound; k <= rightBound; k++) {
44+
if (k <= mid)
45+
if (L[k] == Double.POSITIVE_INFINITY) {
46+
nums[k] = R[j];
47+
j++;
48+
49+
} else if (R[j] == Double.POSITIVE_INFINITY) {
50+
R[k] = L[i];
51+
i++;
52+
53+
} else {
54+
if (L[i] <= R[j]) {
55+
nums[k] = L[i];
56+
i++;
57+
} else {
58+
nums[k] = R[j];
59+
j++;
60+
}
61+
}
62+
}
63+
}
64+
65+
public static void main(String[] args) {
66+
int[] nums = new int[] {11, 5, 3, 2, 5, 7, 8, 9};
67+
System.out.println("Original: " + Arrays.toString(nums));
68+
69+
mergeSort(nums, 0, nums.length - 1);
70+
71+
System.out.println("Sorted: " + Arrays.toString(nums));
72+
}
73+
}

0 commit comments

Comments
 (0)