Skip to content

Commit 3bd4285

Browse files
authored
Merge branch 'master' into refactor/alphabetical
2 parents 2ea2b22 + a6aadd5 commit 3bd4285

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

src/main/java/com/thealgorithms/misc/ShuffleArray.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,37 @@
1717
* @author Rashi Dashore (https://github.com/rashi07dashore)
1818
*/
1919
public final class ShuffleArray {
20-
// Prevent instantiation
20+
2121
private ShuffleArray() {
2222
}
2323

2424
/**
25-
* This method shuffles an array using the Fisher-Yates algorithm.
25+
* Shuffles the provided array in-place using the FisherYates algorithm.
2626
*
27-
* @param arr is the input array to be shuffled
27+
* @param arr the array to shuffle; must not be {@code null}
28+
* @throws IllegalArgumentException if the input array is {@code null}
2829
*/
2930
public static void shuffle(int[] arr) {
31+
if (arr == null) {
32+
throw new IllegalArgumentException("Input array must not be null");
33+
}
34+
3035
Random random = new Random();
3136
for (int i = arr.length - 1; i > 0; i--) {
3237
int j = random.nextInt(i + 1);
38+
swap(arr, i, j);
39+
}
40+
}
41+
42+
/**
43+
* Swaps two elements in an array.
44+
*
45+
* @param arr the array
46+
* @param i index of first element
47+
* @param j index of second element
48+
*/
49+
private static void swap(int[] arr, int i, int j) {
50+
if (i != j) {
3351
int temp = arr[i];
3452
arr[i] = arr[j];
3553
arr[j] = temp;
Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
11
package com.thealgorithms.misc;
22

3-
/*
4-
*A matrix is sparse if many of its coefficients are zero (In general if 2/3rd of matrix elements
5-
*are 0, it is considered as sparse). The interest in sparsity arises because its exploitation can
6-
*lead to enormous computational savings and because many large matrix problems that occur in
7-
*practice are sparse.
3+
/**
4+
* Utility class for calculating the sparsity of a matrix.
5+
* A matrix is considered sparse if a large proportion of its elements are zero.
6+
* Typically, if more than 2/3 of the elements are zero, the matrix is considered sparse.
87
*
9-
* @author Ojasva Jain
8+
* Sparsity is defined as:
9+
* sparsity = (number of zero elements) / (total number of elements)
10+
*
11+
* This can lead to significant computational optimizations.
1012
*/
13+
public final class Sparsity {
1114

12-
final class Sparsity {
1315
private Sparsity() {
1416
}
1517

16-
/*
17-
* @param mat the input matrix
18-
* @return Sparsity of matrix
19-
*
20-
* where sparsity = number of zeroes/total elements in matrix
18+
/**
19+
* Calculates the sparsity of a given 2D matrix.
2120
*
21+
* @param matrix the input matrix
22+
* @return the sparsity value between 0 and 1
23+
* @throws IllegalArgumentException if the matrix is null, empty, or contains empty rows
2224
*/
23-
static double sparsity(double[][] mat) {
24-
if (mat == null || mat.length == 0) {
25+
public static double sparsity(double[][] matrix) {
26+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
2527
throw new IllegalArgumentException("Matrix cannot be null or empty");
2628
}
2729

28-
int zero = 0;
29-
// Traversing the matrix to count number of zeroes
30-
for (int i = 0; i < mat.length; i++) {
31-
for (int j = 0; j < mat[i].length; j++) {
32-
if (mat[i][j] == 0) {
33-
zero++;
30+
int zeroCount = 0;
31+
int totalElements = 0;
32+
33+
// Count the number of zero elements and total elements
34+
for (double[] row : matrix) {
35+
for (double value : row) {
36+
if (value == 0.0) {
37+
zeroCount++;
3438
}
39+
totalElements++;
3540
}
3641
}
37-
// return sparsity
38-
return ((double) zero / (mat.length * mat[0].length));
42+
43+
// Return sparsity as a double
44+
return (double) zeroCount / totalElements;
3945
}
4046
}

0 commit comments

Comments
 (0)