Refactored the BinarySearch.Java file#370
Open
kbashadal wants to merge 3 commits intowilliamfiset:masterfrom
Open
Refactored the BinarySearch.Java file#370kbashadal wants to merge 3 commits intowilliamfiset:masterfrom
kbashadal wants to merge 3 commits intowilliamfiset:masterfrom
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…name method/variable refactoring**