Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c4dab39

Browse files
Merge pull request #141 from gauravdarbhanga/patch-3
Comments on BinarySearch.java
2 parents 1162a5e + 71d8b30 commit c4dab39

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

‎src/com/jwetherell/algorithms/search/BinarySearch.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public static final int find(int value, int[] array, boolean optimize) {
2929
BinarySearch.sorted = null;
3030
}
3131
}
32-
32+
//Recursively find the element
33+
//@return find the element value by recursively
3334
private static int recursiveFind(int value, int start, int end, boolean optimize) {
3435
if (start == end) {
3536
int lastValue = sorted[start]; // start==end
@@ -43,8 +44,10 @@ private static int recursiveFind(int value, int start, int end, boolean optimize
4344
final int middle = low + ((high - low) / 2);
4445

4546
final int middleValue = sorted[middle];
47+
//checks if the middle index is element
4648
if (value == middleValue)
4749
return middle;
50+
//if value is greater than move to right
4851
if (value > middleValue) {
4952
if (optimize && (end - middle) <= SWITCH_TO_BRUTE_FORCE)
5053
return linearSearch(value, middle + 1, end);
@@ -54,7 +57,10 @@ private static int recursiveFind(int value, int start, int end, boolean optimize
5457
return linearSearch(value, start, middle - 1);
5558
return recursiveFind(value, start, middle - 1, optimize);
5659
}
57-
60+
//Linear search to find the element.
61+
//@value the element we want to find.
62+
//@start first index of the array in the array
63+
//@end last index of the array in the array.
5864
private static final int linearSearch(int value, int start, int end) {
5965
// From index i = start to i = end check if value matches sorted[i]
6066
for (int i = start; i <= end; i++) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /