Skip to content
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

Refactored the BinarySearch.Java file #370

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kbashadal
Copy link

…name method/variable refactoring**

2. Renamed the EPS constant to EPSILON to improve readability.-->**Introduce explaining variable refactoring**
3. Added Javadoc comments for the binarySearch method to improve documentation.
4. Changed the IllegalArgumentException message to make it more clear.
5. Changed the name of the function used in the first example from function to squareFunction to make it more descriptive.
6. Added a comment to indicate that any function can be used for the binary search.
7. Added a comment to explain that the second example is not practical but serves as an illustration.
8. Used Math.PI instead of a hardcoded value for pi in the second example.

…name method/variable refactoring**

    2. Renamed the EPS constant to EPSILON to improve readability.-->**Introduce explaining variable refactoring**
    3. Added Javadoc comments for the binarySearch method to improve documentation.
    4. Changed the IllegalArgumentException message to make it more clear.
    5. Changed the name of the function used in the first example from function to squareFunction to make it more descriptive.
    6. Added a comment to indicate that any function can be used for the binary search.
    7. Added a comment to explain that the second example is not practical but serves as an illustration.
    8. Used Math.PI instead of a hardcoded value for pi in the second example.
…n statement for when the value is found at

   the mid-point element. This is to avoid having to check for equality in the while loop condition.-->
   2. The loop condition has been simplified to only check if the target value is within the bounds of the list,
   and to loop while the lower bound index is less than or equal to the upper bound index.
   3. The mid variable is now declared inside the loop for clarity, and the mid assignment has been updated
   to interpolate the index of the mid-point element. This is done by calculating a weighted average of the
   indices using the formula lo + ((val - nums[lo]) * (hi - lo)) / (nums[hi] - nums[lo]). --> ****Decompose conditional****
   4. This gives a better estimate of the index to search next, assuming that the list contains uniformly distributed values.
   5. The if-else condition for updating the lower and upper bound indices has been simplified to a ternary operator for readability.
…d signature to avoid unnecessary boxing and unboxing of primitive types.

   2. Changed the name of the EPS variable to EPSILON to follow Java naming conventions.-->****Introduce explaining variable refactoring****
   3. Changed Double best = null to double best = Double.NaN to avoid null pointer exceptions and improve readability.
   4. Added .0 to the division operations to ensure that the results are doubles.
   5. Changed the if (res1 > res2) statement to use braces for clarity and consistency.
   6. Changed if (best != null && Math.abs(best - mid1) < EPS) to if (&& Math.abs(best - mid1) < EPSILON) to avoid null pointer exceptions and improve readability.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant