Skip to content

Improved code readability and code quality #4663

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

Merged
merged 9 commits into from
Oct 11, 2023
2 changes: 1 addition & 1 deletion src/main/java/com/thealgorithms/maths/Average.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static double average(double[] numbers) {
}

/**
* find average value of int array
* find average value of an int array
*
* @param numbers the array contains element and the sum does not excess long
* value limit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/*
* @author Ojasva Jain
* Determinant of Matrix Wikipedia link : https://en.wikipedia.org/wiki/Determinant
* Determinant of a Matrix Wikipedia link: https://en.wikipedia.org/wiki/Determinant
*/
public class DeterminantOfMatrix {

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/thealgorithms/maths/DigitalRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ public static int digitalRoot(int n) {
}
}

// This function is used for finding the sum of digits of number
// This function is used for finding the sum of the digits of number
public static int single(int n) {
if (n <= 9) { // if n becomes less than 10 than return n
return n;
} else {
return (n % 10) + single(n / 10); // n % 10 for extracting digits one by one
}
} // n / 10 is the number obtainded after removing the digit one by one
// Sum of digits is stored in the Stack memory and then finally returned
} // n / 10 is the number obtained after removing the digit one by one
// The Sum of digits is stored in the Stack memory and then finally returned
}
/**
* Time Complexity : O((Number of Digits)^2) Auxiliary Space Complexity :
* O(Number of Digits) Constraints : 1 <= n <= 10^7
* Time Complexity: O((Number of Digits)^2) Auxiliary Space Complexity:
* O(Number of Digits) Constraints: 1 <= n <= 10^7
*/
4 changes: 2 additions & 2 deletions src/main/java/com/thealgorithms/maths/DistanceFormula.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static int hammingDistance(int[] b1, int[] b2) {
int d = 0;

if (b1.length != b2.length) {
return -1; // error, both array must be have the same length
return -1; // error, both arrays must have the same length
}

for (int i = 0; i < b1.length; i++) {
Expand All @@ -31,7 +31,7 @@ public static double minkowskiDistance(double[] p1, double[] p2, int p) {
double distance = 0.0;

if (p1.length != p2.length) {
return -1; // error, both array must be have the same length
return -1; // error, both arrays must have the same length
}

for (int i = 0; i < p1.length; i++) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/thealgorithms/maths/DudeneyNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ public static boolean isDudeney(int n) {
if (cube_root * cube_root * cube_root != n) {
return false;
}
int sum_of_digits = 0; // Stores the sums of the digit of the entered number
int sum_of_digits = 0; // Stores the sums of the digits of the entered number
int temp = n; // A temporary variable to store the entered number
// Loop to calculate sum of the digits.
// Loop to calculate the sum of the digits.
while (temp > 0) {
// Extracting Last digit of the number
// Extracting the Last digit of the number
int rem = temp % 10;

// Calculating sum of digits.
// Calculating the sum of digits.
sum_of_digits += rem;

// Removing the last digit
temp /= 10;
}

// If the cube root of the number is not equal to the sum of its digits we return false.
// If the cube root of the number is not equal to the sum of its digits, we return false.
return cube_root == sum_of_digits;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/thealgorithms/maths/EulerMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static ArrayList<double[]> eulerFull(double xStart, double xEnd, double s
double xCurrent = xStart;

while (xCurrent < xEnd) {
// Euler method for next step
// Euler's method for next step
yCurrent = eulerStep(xCurrent, stepSize, yCurrent, differentialEquation);
xCurrent += stepSize;
double[] point = {xCurrent, yCurrent};
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/thealgorithms/maths/FindKthNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class FindKthNumber {
private static final Random random = new Random();

public static void main(String[] args) {
/* generate array with random size and random elements */
/* generate an array with random size and random elements */
int[] nums = generateArray(100);

/* get 3th largest element */
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/thealgorithms/maths/FindMax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ private FindMax() {
* @return the maximum value stored in the input array
*/
public static int findMax(final int[] array) {
if (array.length == 0) {
throw new IllegalArgumentException("array must be non-empty.");
int n = array.length;
if (n == 0) {
throw new IllegalArgumentException("Array must be non-empty.");
}
int max = array[0];
for (int i = 1; i < array.length; i++) {
for (int i = 1; i < n; i++) {
if (array[i] > max) {
max = array[i];
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/thealgorithms/maths/FindMaxRecursion.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public final class FindMaxRecursion {
private FindMaxRecursion() {
}
/**
* Get max of array using divide and conquer algorithm
* Get max of an array using divide and conquer algorithm
*
* @param array contains elements
* @param low the index of the first element
Expand All @@ -14,7 +14,7 @@ private FindMaxRecursion() {
*/
public static int max(final int[] array, final int low, final int high) {
if (array.length == 0) {
throw new IllegalArgumentException("array must be non-empty.");
throw new IllegalArgumentException("Array must be non-empty.");
}
if (low == high) {
return array[low]; // or array[high]
Expand All @@ -25,11 +25,11 @@ public static int max(final int[] array, final int low, final int high) {
int leftMax = max(array, low, mid); // get max in [low, mid]
int rightMax = max(array, mid + 1, high); // get max in [mid+1, high]

return leftMax < rightMax ? rightMax : leftMax;
return Math.max(leftMax, rightMax);
}

/**
* Get max of array using recursion algorithm
* Get max of an array using recursion algorithm
*
* @param array contains elements
* @return max value of {@code array}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/thealgorithms/maths/FindMin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private FindMin() {
*/
public static int findMin(final int[] array) {
if (array.length == 0) {
throw new IllegalArgumentException("array must be non-empty.");
throw new IllegalArgumentException("Array must be non-empty.");
}
int min = array[0];
for (int i = 1; i < array.length; i++) {
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/thealgorithms/maths/FindMinRecursion.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ public final class FindMinRecursion {

private FindMinRecursion() {
}

/**
* Get min of an array using divide and conquer algorithm
*
* @param array contains elements
* @param low the index of the first element
* @param high the index of the last element
* @return min of {@code array}
*/

public static int min(final int[] array, final int low, final int high) {
if (array.length == 0) {
throw new IllegalArgumentException("array must be non-empty.");
}

if (low == high) {
return array[low]; // or array[high]
}
Expand All @@ -17,14 +28,13 @@ public static int min(final int[] array, final int low, final int high) {
int leftMin = min(array, low, mid); // get min in [low, mid]
int rightMin = min(array, mid + 1, high); // get min in [mid+1, high]

return leftMin > rightMin ? rightMin : leftMin;
return Math.min(leftMin, rightMin);
}

/**
* Get min of array using recursion algorithm
* Get min of an array using recursion algorithm
*
* @param array contains elements
* @param len length of given array
* @return min value of {@code array}
*/
public static int min(final int[] array) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/thealgorithms/maths/GCD.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.thealgorithms.maths;

/**
* This is Euclid's algorithm which is used to find the greatest common
* denominator Overide function name gcd
* This is Euclid's algorithm, used to find the greatest common
* denominator Override function name gcd
*
* @author Oskar Enmalm 3/10/17
*/
public class GCD {

/**
* get greatest common divisor
* get the greatest common divisor
*
* @param num1 the first number
* @param num2 the second number
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/thealgorithms/maths/Gaussian.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static double[][] gaussianElimination(int mat_size, int i, double[][] mat
return mat;
}

// calculate the x_1, x_2,... values of the gaussian and save it in an arraylist.
// calculate the x_1, x_2, ... values of the gaussian and save it in an arraylist.
public static ArrayList<Double> valueOfGaussian(int mat_size, double[][] x, double[][] mat) {
ArrayList<Double> answerArray = new ArrayList<Double>();
int i, j;
Expand Down