Skip to content

Commit

Permalink
Merge pull request #399 from JackW2000/master
Browse files Browse the repository at this point in the history
feat: added java binary search snippet
  • Loading branch information
Kavya-24 authored Oct 6, 2024
2 parents 6200801 + 6143dc8 commit 5da1b7d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Java/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package java;

public class BinarySearch {

// Pass in the array, the value to search for,
// the index of the start of the array, and the index of the end of the array
// The passing of index values will allow the same array to be utilised throughout each
// recursion, "filtering" out values that have been checked against by only looking between the indices
public static int binarySearch(int[] arr, int searchFor, int startIndex, int endIndex) {
// Binary search: (Assumes array is sorted) Splits the array in half using the midpoint,
// halving the data to look at each time by ignoring values that are less than or larger than
// the value being searched for.
// Time Complexity: Best - O(1) Average - O(log(n)) Worst - O(log(n))

// Check that there is more than one value in the array (low != high)
if (endIndex >= startIndex) {
int mid = startIndex + (endIndex - startIndex) / 2;

// Check if searchFor is in the middle, if it is then return the index
if (arr[mid] == searchFor) {
return mid;
}

// Check if the value is less than that at the mid, if it is, only look at the values to the left
// If not, the value is either to the right or not present so look at the right half instead
if (arr[mid] > searchFor) {
return binarySearch(arr, searchFor, startIndex, mid - 1);
} else {
return binarySearch(arr, searchFor, mid + 1, endIndex);
}
}

// If the value is not found, return -1
return -1;
}
}

0 comments on commit 5da1b7d

Please sign in to comment.