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:
parent
17fe4298b6
commit
152e29034d
@ -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
|
* @param numbers the array contains element and the sum does not excess long
|
||||||
* value limit
|
* value limit
|
||||||
|
@ -4,7 +4,7 @@ import java.util.*;
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* @author Ojasva Jain
|
* @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 {
|
public class DeterminantOfMatrix {
|
||||||
|
|
||||||
|
@ -51,15 +51,15 @@ 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) {
|
public static int single(int n) {
|
||||||
if (n <= 9) { // if n becomes less than 10 than return n
|
if (n <= 9) { // if n becomes less than 10 than return n
|
||||||
return n;
|
return n;
|
||||||
} else {
|
} else {
|
||||||
return (n % 10) + single(n / 10); // n % 10 for extracting digits one by one
|
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
|
} // n / 10 is the number obtained after removing the digit one by one
|
||||||
// Sum of digits is stored in the Stack memory and then finally returned
|
// The Sum of digits is stored in the Stack memory and then finally returned
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Time Complexity: O((Number of Digits)^2) Auxiliary Space Complexity:
|
* Time Complexity: O((Number of Digits)^2) Auxiliary Space Complexity:
|
||||||
|
@ -16,7 +16,7 @@ public class DistanceFormula {
|
|||||||
int d = 0;
|
int d = 0;
|
||||||
|
|
||||||
if (b1.length != b2.length) {
|
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++) {
|
for (int i = 0; i < b1.length; i++) {
|
||||||
@ -31,7 +31,7 @@ public class DistanceFormula {
|
|||||||
double distance = 0.0;
|
double distance = 0.0;
|
||||||
|
|
||||||
if (p1.length != p2.length) {
|
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++) {
|
for (int i = 0; i < p1.length; i++) {
|
||||||
|
@ -16,21 +16,21 @@ public class DudeneyNumber {
|
|||||||
if (cube_root * cube_root * cube_root != n) {
|
if (cube_root * cube_root * cube_root != n) {
|
||||||
return false;
|
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
|
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) {
|
while (temp > 0) {
|
||||||
// Extracting Last digit of the number
|
// Extracting the Last digit of the number
|
||||||
int rem = temp % 10;
|
int rem = temp % 10;
|
||||||
|
|
||||||
// Calculating sum of digits.
|
// Calculating the sum of digits.
|
||||||
sum_of_digits += rem;
|
sum_of_digits += rem;
|
||||||
|
|
||||||
// Removing the last digit
|
// Removing the last digit
|
||||||
temp /= 10;
|
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;
|
return cube_root == sum_of_digits;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,7 @@ public class EulerMethod {
|
|||||||
double xCurrent = xStart;
|
double xCurrent = xStart;
|
||||||
|
|
||||||
while (xCurrent < xEnd) {
|
while (xCurrent < xEnd) {
|
||||||
// Euler method for next step
|
// Euler's method for next step
|
||||||
yCurrent = eulerStep(xCurrent, stepSize, yCurrent, differentialEquation);
|
yCurrent = eulerStep(xCurrent, stepSize, yCurrent, differentialEquation);
|
||||||
xCurrent += stepSize;
|
xCurrent += stepSize;
|
||||||
double[] point = {xCurrent, yCurrent};
|
double[] point = {xCurrent, yCurrent};
|
||||||
|
@ -11,7 +11,7 @@ public class FindKthNumber {
|
|||||||
private static final Random random = new Random();
|
private static final Random random = new Random();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
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);
|
int[] nums = generateArray(100);
|
||||||
|
|
||||||
/* get 3th largest element */
|
/* get 3th largest element */
|
||||||
|
@ -12,11 +12,12 @@ public final class FindMax {
|
|||||||
* @return the maximum value stored in the input array
|
* @return the maximum value stored in the input array
|
||||||
*/
|
*/
|
||||||
public static int findMax(final int[] array) {
|
public static int findMax(final int[] array) {
|
||||||
if (array.length == 0) {
|
int n = array.length;
|
||||||
throw new IllegalArgumentException("array must be non-empty.");
|
if (n == 0) {
|
||||||
|
throw new IllegalArgumentException("Array must be non-empty.");
|
||||||
}
|
}
|
||||||
int max = array[0];
|
int max = array[0];
|
||||||
for (int i = 1; i < array.length; i++) {
|
for (int i = 1; i < n; i++) {
|
||||||
if (array[i] > max) {
|
if (array[i] > max) {
|
||||||
max = array[i];
|
max = array[i];
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ public final class FindMaxRecursion {
|
|||||||
private 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 array contains elements
|
||||||
* @param low the index of the first element
|
* @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) {
|
public static int max(final int[] array, final int low, final int high) {
|
||||||
if (array.length == 0) {
|
if (array.length == 0) {
|
||||||
throw new IllegalArgumentException("array must be non-empty.");
|
throw new IllegalArgumentException("Array must be non-empty.");
|
||||||
}
|
}
|
||||||
if (low == high) {
|
if (low == high) {
|
||||||
return array[low]; // or array[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 leftMax = max(array, low, mid); // get max in [low, mid]
|
||||||
int rightMax = max(array, mid + 1, high); // get max in [mid+1, high]
|
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
|
* @param array contains elements
|
||||||
* @return max value of {@code array}
|
* @return max value of {@code array}
|
||||||
|
@ -13,7 +13,7 @@ public final class FindMin {
|
|||||||
*/
|
*/
|
||||||
public static int findMin(final int[] array) {
|
public static int findMin(final int[] array) {
|
||||||
if (array.length == 0) {
|
if (array.length == 0) {
|
||||||
throw new IllegalArgumentException("array must be non-empty.");
|
throw new IllegalArgumentException("Array must be non-empty.");
|
||||||
}
|
}
|
||||||
int min = array[0];
|
int min = array[0];
|
||||||
for (int i = 1; i < array.length; i++) {
|
for (int i = 1; i < array.length; i++) {
|
||||||
|
@ -4,6 +4,16 @@ public final class FindMinRecursion {
|
|||||||
|
|
||||||
private 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) {
|
public static int min(final int[] array, final int low, final int high) {
|
||||||
if (array.length == 0) {
|
if (array.length == 0) {
|
||||||
throw new IllegalArgumentException("array must be non-empty.");
|
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 leftMin = min(array, low, mid); // get min in [low, mid]
|
||||||
int rightMin = min(array, mid + 1, high); // get min in [mid+1, high]
|
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 array contains elements
|
||||||
* @param len length of given array
|
|
||||||
* @return min value of {@code array}
|
* @return min value of {@code array}
|
||||||
*/
|
*/
|
||||||
public static int min(final int[] array) {
|
public static int min(final int[] array) {
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is Euclid's algorithm which is used to find the greatest common
|
* This is Euclid's algorithm, used to find the greatest common
|
||||||
* denominator Overide function name gcd
|
* denominator Override function name gcd
|
||||||
*
|
*
|
||||||
* @author Oskar Enmalm 3/10/17
|
* @author Oskar Enmalm 3/10/17
|
||||||
*/
|
*/
|
||||||
public class GCD {
|
public class GCD {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get greatest common divisor
|
* get the greatest common divisor
|
||||||
*
|
*
|
||||||
* @param num1 the first number
|
* @param num1 the first number
|
||||||
* @param num2 the second number
|
* @param num2 the second number
|
||||||
|
Loading…
Reference in New Issue
Block a user