diff --git a/Java/Data Structures/Arrays/StrassensAlgorithm.txt b/Java/Data Structures/Arrays/StrassensAlgorithm.txt new file mode 100644 index 0000000..5f129e1 --- /dev/null +++ b/Java/Data Structures/Arrays/StrassensAlgorithm.txt @@ -0,0 +1,92 @@ +public class StrassensAlgorithm { + + public static int[][] multiply(int[][] A, int[][] B) { + int n = A.length; + int[][] C = new int[n][n]; + if (n == 1) + C[0][0] = A[0][0] * B[0][0]; + else { + int[][] A11 = new int[n / 2][n / 2]; + int[][] A12 = new int[n / 2][n / 2]; + int[][] A21 = new int[n / 2][n / 2]; + int[][] A22 = new int[n / 2][n / 2]; + + int[][] B11 = new int[n / 2][n / 2]; + int[][] B12 = new int[n / 2][n / 2]; + int[][] B21 = new int[n / 2][n / 2]; + int[][] B22 = new int[n / 2][n / 2]; + + split(A, A11, 0, 0); + split(A, A12, 0, n / 2); + split(A, A21, n / 2, 0); + split(A, A22, n / 2, n / 2); + + split(B, B11, 0, 0); + split(B, B12, 0, n / 2); + split(B, B21, n / 2, 0); + split(B, B22, n / 2, n / 2); + + int[][] M1 = multiply(add(A11, A22), add(B11, B22)); + int[][] M2 = multiply(add(A21, A22), B11); + int[][] M3 = multiply(A11, sub(B12, B22)); + int[][] M4 = multiply(A22, sub(B21, B11)); + int[][] M5 = multiply(add(A11, A12), B22); + int[][] M6 = multiply(sub(A21, A11), add(B11, B12)); + int[][] M7 = multiply(sub(A12, A22), add(B21, B22)); + + int[][] C11 = add(sub(add(M1, M4), M5), M7); + int[][] C12 = add(M3, M5); + int[][] C21 = add(M2, M4); + int[][] C22 = add(sub(add(M1, M3), M2), M6); + + join(C11, C, 0, 0); + join(C12, C, 0, n / 2); + join(C21, C, n / 2, 0); + join(C22, C, n / 2, n / 2); + } + return C; + } + + public static void split(int[][] P, int[][] C, int iB, int jB) { + for (int i1 = 0, i2 = iB; i1 < C.length; i1++, i2++) + for (int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++) + C[i1][j1] = P[i2][j2]; + } + + public static int[][] add(int[][] A, int[][] B) { + int n = A.length; + int[][] C = new int[n][n]; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + C[i][j] = A[i][j] + B[i][j]; + return C; + } + + public static int[][] sub(int[][] A, int[][] B) { + int n = A.length; + int[][] C = new int[n][n]; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + C[i][j] = A[i][j] - B[i][j]; + return C; + } + + public static void join(int[][] C, int[][] P, int iB, int jB) { + for (int i1 = 0, i2 = iB; i1 < C.length; i1++, i2++) + for (int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++) + P[i2][j2] = C[i1][j1]; + } + + public static void main(String[] args) { + int[][] A = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; + int[][] B = {{17, 18, 19, 20}, {21, 22, 23, 24}, {25, 26, 27, 28}, {29, 30, 31, 32}}; + + int[][] C = multiply(A, B); + for (int i = 0; i < C.length; i++) { + for (int j = 0; j < C[0].length; j++) { + System.out.print(C[i][j] + " "); + } + System.out.println(); + } + } +} diff --git a/Java/sorting/Radix.txt b/Java/sorting/Radix.txt new file mode 100644 index 0000000..7818c2a --- /dev/null +++ b/Java/sorting/Radix.txt @@ -0,0 +1,76 @@ +import java.util.Arrays; + +public class Radix { + + // A utility function to get the maximum value in arr[] + static int getMax(int arr[]) { + int max = arr[0]; + for (int i = 1; i < arr.length; i++) + if (arr[i] > max) + max = arr[i]; + return max; + } + + // A function to do counting sort of arr[] according to + // the digit represented by exp. + static void countSort(int arr[], int exp) { + int n = arr.length; + int output[] = new int[n]; + int count[] = new int[10]; + Arrays.fill(count, 0); + + // Store count of occurrences in count[] + for (int i = 0; i < n; i++) + count[(arr[i] / exp) % 10]++; + + // Change count[i] so that count[i] now contains + // actual position of this digit in output[] + for (int i = 1; i < 10; i++) + count[i] += count[i - 1]; + + // Build the output array + for (int i = n - 1; i >= 0; i--) { + output[count[(arr[i] / exp) % 10] - 1] = arr[i]; + count[(arr[i] / exp) % 10]--; + } + + // Copy the output array to arr[], so that arr[] now + // contains sorted numbers according to the current digit + for (int i = 0; i < n; i++) + arr[i] = output[i]; + } + + // The main function to that sorts arr[] using Radix Sort + static void radixSort(int arr[]) { + int max = getMax(arr); + + // Do counting sort for every digit. Note that instead + // of passing digit number, exp is passed. exp is 10^i + // where i is current digit number + for (int exp = 1; max / exp > 0; exp *= 10) + countSort(arr, exp); + } + + // A utility function to print an array + static void printArray(int arr[]) { + for (int i : arr) System.out.print(i + " "); + System.out.println(); + } + + // Driver code + public static void main(String[] args) { + int arr[] = {170, 45, 75, 90, 802, 24, 2, 66}; + System.out.println("Original array:"); + printArray(arr); + + radixSort(arr); + + System.out.println("\nSorted array:"); + printArray(arr); + } +} + +// Original array: +170 45 75 90 802 24 2 66 +// Sorted array: +2 24 45 66 75 90 170 802