Skip to content

Commit 152e290

Browse files
Improved code readability and code quality (#4663)
* Fixed Small typos :-) * Update BufferedReader.java * Made the following changes : * Improved readability of files and removed gramatical errors. * Implemented data assigning instead of manually calling arr.ylength in several instances like FindMax, FindMaxRecursion etc. * Removed unwanted params from several files * Implemented Math methods in files math/FindMinRecursion.java and FindMaxRecursion.java * Update src/main/java/com/thealgorithms/maths/FindMinRecursion.java --------- Co-authored-by: Debasish Biswas <[email protected]>
1 parent 17fe429 commit 152e290

13 files changed

+41
-31
lines changed

src/main/java/com/thealgorithms/maths/Average.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static double average(double[] numbers) {
2323
}
2424

2525
/**
26-
* find average value of int array
26+
* find average value of an int array
2727
*
2828
* @param numbers the array contains element and the sum does not excess long
2929
* value limit

src/main/java/com/thealgorithms/maths/DeterminantOfMatrix.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/*
66
* @author Ojasva Jain
7-
* Determinant of Matrix Wikipedia link : https://en.wikipedia.org/wiki/Determinant
7+
* Determinant of a Matrix Wikipedia link: https://en.wikipedia.org/wiki/Determinant
88
*/
99
public class DeterminantOfMatrix {
1010

src/main/java/com/thealgorithms/maths/DigitalRoot.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ public static int digitalRoot(int n) {
5151
}
5252
}
5353

54-
// This function is used for finding the sum of digits of number
54+
// This function is used for finding the sum of the digits of number
5555
public static int single(int n) {
5656
if (n <= 9) { // if n becomes less than 10 than return n
5757
return n;
5858
} else {
5959
return (n % 10) + single(n / 10); // n % 10 for extracting digits one by one
6060
}
61-
} // n / 10 is the number obtainded after removing the digit one by one
62-
// Sum of digits is stored in the Stack memory and then finally returned
61+
} // n / 10 is the number obtained after removing the digit one by one
62+
// The Sum of digits is stored in the Stack memory and then finally returned
6363
}
6464
/**
65-
* Time Complexity : O((Number of Digits)^2) Auxiliary Space Complexity :
66-
* O(Number of Digits) Constraints : 1 <= n <= 10^7
65+
* Time Complexity: O((Number of Digits)^2) Auxiliary Space Complexity:
66+
* O(Number of Digits) Constraints: 1 <= n <= 10^7
6767
*/

src/main/java/com/thealgorithms/maths/DistanceFormula.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static int hammingDistance(int[] b1, int[] b2) {
1616
int d = 0;
1717

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

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

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

3737
for (int i = 0; i < p1.length; i++) {

src/main/java/com/thealgorithms/maths/DudeneyNumber.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ public static boolean isDudeney(int n) {
1616
if (cube_root * cube_root * cube_root != n) {
1717
return false;
1818
}
19-
int sum_of_digits = 0; // Stores the sums of the digit of the entered number
19+
int sum_of_digits = 0; // Stores the sums of the digits of the entered number
2020
int temp = n; // A temporary variable to store the entered number
21-
// Loop to calculate sum of the digits.
21+
// Loop to calculate the sum of the digits.
2222
while (temp > 0) {
23-
// Extracting Last digit of the number
23+
// Extracting the Last digit of the number
2424
int rem = temp % 10;
2525

26-
// Calculating sum of digits.
26+
// Calculating the sum of digits.
2727
sum_of_digits += rem;
2828

2929
// Removing the last digit
3030
temp /= 10;
3131
}
3232

33-
// If the cube root of the number is not equal to the sum of its digits we return false.
33+
// If the cube root of the number is not equal to the sum of its digits, we return false.
3434
return cube_root == sum_of_digits;
3535
}
3636
}

src/main/java/com/thealgorithms/maths/EulerMethod.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static ArrayList<double[]> eulerFull(double xStart, double xEnd, double s
8787
double xCurrent = xStart;
8888

8989
while (xCurrent < xEnd) {
90-
// Euler method for next step
90+
// Euler's method for next step
9191
yCurrent = eulerStep(xCurrent, stepSize, yCurrent, differentialEquation);
9292
xCurrent += stepSize;
9393
double[] point = {xCurrent, yCurrent};

src/main/java/com/thealgorithms/maths/FindKthNumber.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class FindKthNumber {
1111
private static final Random random = new Random();
1212

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

1717
/* get 3th largest element */

src/main/java/com/thealgorithms/maths/FindMax.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ private FindMax() {
1212
* @return the maximum value stored in the input array
1313
*/
1414
public static int findMax(final int[] array) {
15-
if (array.length == 0) {
16-
throw new IllegalArgumentException("array must be non-empty.");
15+
int n = array.length;
16+
if (n == 0) {
17+
throw new IllegalArgumentException("Array must be non-empty.");
1718
}
1819
int max = array[0];
19-
for (int i = 1; i < array.length; i++) {
20+
for (int i = 1; i < n; i++) {
2021
if (array[i] > max) {
2122
max = array[i];
2223
}

src/main/java/com/thealgorithms/maths/FindMaxRecursion.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public final class FindMaxRecursion {
55
private FindMaxRecursion() {
66
}
77
/**
8-
* Get max of array using divide and conquer algorithm
8+
* Get max of an array using divide and conquer algorithm
99
*
1010
* @param array contains elements
1111
* @param low the index of the first element
@@ -14,7 +14,7 @@ private FindMaxRecursion() {
1414
*/
1515
public static int max(final int[] array, final int low, final int high) {
1616
if (array.length == 0) {
17-
throw new IllegalArgumentException("array must be non-empty.");
17+
throw new IllegalArgumentException("Array must be non-empty.");
1818
}
1919
if (low == high) {
2020
return array[low]; // or array[high]
@@ -25,11 +25,11 @@ public static int max(final int[] array, final int low, final int high) {
2525
int leftMax = max(array, low, mid); // get max in [low, mid]
2626
int rightMax = max(array, mid + 1, high); // get max in [mid+1, high]
2727

28-
return leftMax < rightMax ? rightMax : leftMax;
28+
return Math.max(leftMax, rightMax);
2929
}
3030

3131
/**
32-
* Get max of array using recursion algorithm
32+
* Get max of an array using recursion algorithm
3333
*
3434
* @param array contains elements
3535
* @return max value of {@code array}

src/main/java/com/thealgorithms/maths/FindMin.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private FindMin() {
1313
*/
1414
public static int findMin(final int[] array) {
1515
if (array.length == 0) {
16-
throw new IllegalArgumentException("array must be non-empty.");
16+
throw new IllegalArgumentException("Array must be non-empty.");
1717
}
1818
int min = array[0];
1919
for (int i = 1; i < array.length; i++) {

src/main/java/com/thealgorithms/maths/FindMinRecursion.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ public final class FindMinRecursion {
44

55
private FindMinRecursion() {
66
}
7+
8+
/**
9+
* Get min of an array using divide and conquer algorithm
10+
*
11+
* @param array contains elements
12+
* @param low the index of the first element
13+
* @param high the index of the last element
14+
* @return min of {@code array}
15+
*/
16+
717
public static int min(final int[] array, final int low, final int high) {
818
if (array.length == 0) {
919
throw new IllegalArgumentException("array must be non-empty.");
@@ -17,14 +27,13 @@ public static int min(final int[] array, final int low, final int high) {
1727
int leftMin = min(array, low, mid); // get min in [low, mid]
1828
int rightMin = min(array, mid + 1, high); // get min in [mid+1, high]
1929

20-
return leftMin > rightMin ? rightMin : leftMin;
30+
return Math.min(leftMin, rightMin);
2131
}
2232

2333
/**
24-
* Get min of array using recursion algorithm
34+
* Get min of an array using recursion algorithm
2535
*
2636
* @param array contains elements
27-
* @param len length of given array
2837
* @return min value of {@code array}
2938
*/
3039
public static int min(final int[] array) {

src/main/java/com/thealgorithms/maths/GCD.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.thealgorithms.maths;
22

33
/**
4-
* This is Euclid's algorithm which is used to find the greatest common
5-
* denominator Overide function name gcd
4+
* This is Euclid's algorithm, used to find the greatest common
5+
* denominator Override function name gcd
66
*
77
* @author Oskar Enmalm 3/10/17
88
*/
99
public class GCD {
1010

1111
/**
12-
* get greatest common divisor
12+
* get the greatest common divisor
1313
*
1414
* @param num1 the first number
1515
* @param num2 the second number

src/main/java/com/thealgorithms/maths/Gaussian.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static double[][] gaussianElimination(int mat_size, int i, double[][] mat
3838
return mat;
3939
}
4040

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

0 commit comments

Comments
 (0)