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 <debasishbsws.dev@gmail.com>
This commit is contained in:
Abhinav Pandey 2023-10-11 17:29:55 +05:30 committed by GitHub
parent 17fe4298b6
commit 152e29034d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 41 additions and 31 deletions

View File

@ -23,7 +23,7 @@ public class Average {
}
/**
* 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

View File

@ -4,7 +4,7 @@ import java.util.*;
/*
* @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 {

View File

@ -51,17 +51,17 @@ class DigitalRoot {
}
}
// 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
*/

View File

@ -16,7 +16,7 @@ public class DistanceFormula {
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++) {
@ -31,7 +31,7 @@ public class DistanceFormula {
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++) {

View File

@ -16,21 +16,21 @@ public class DudeneyNumber {
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;
}
}

View File

@ -87,7 +87,7 @@ public class EulerMethod {
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};

View File

@ -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 */

View File

@ -12,11 +12,12 @@ public final class 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];
}

View File

@ -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
@ -14,7 +14,7 @@ public final class 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]
@ -25,11 +25,11 @@ public final class FindMaxRecursion {
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}

View File

@ -13,7 +13,7 @@ public final class 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++) {

View File

@ -4,6 +4,16 @@ 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.");
@ -17,14 +27,13 @@ public final class FindMinRecursion {
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) {

View File

@ -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

View File

@ -38,7 +38,7 @@ public class Gaussian {
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;