diff --git a/code/languages/Java/README_binary_search.md b/code/languages/Java/README_binary_search.md new file mode 100644 index 0000000000..f9046570d7 --- /dev/null +++ b/code/languages/Java/README_binary_search.md @@ -0,0 +1,3 @@ +Binary search is a method for finding the index of an element in an already-sorted array. It works by repeatedly halving the array, picking the half that contains our element based on whether the middle element is lesser or greater than it, until the element is found. It's one of the simplest examples of a "divide and conquer" algorithm, which is an algorithm that repeatedly reduces the problem into smaller sub-problems until a solution is found. + +Binary search runs in O(log n) time. diff --git a/code/languages/Java/binary_search.java b/code/languages/Java/binary_search.java new file mode 100644 index 0000000000..7330773c14 --- /dev/null +++ b/code/languages/Java/binary_search.java @@ -0,0 +1,56 @@ +class BinarySearch { + public static int binarySearchInt(int[] arr, int x) { + // Initialize the variables. start and end define our search space + int start = 0; + int end = arr.length - 1; + int midpoint; + // The main loop. Each iteration we cut our search space in half + while (start <= end) { + // Check the midpoint of the search space + midpoint = start + ((end - start)/2); + // If the midpoint is what we're looking for, return + if (arr[midpoint] == x) { + return midpoint; + } + // If the midpoint is too large, search the first half + if (arr[midpoint] > x) { + end = midpoint - 1; + } + // If the midpoint is too small, search the second half + else { + start = midpoint + 1; + } + } + // If we never find the element, it is not in the array. In that case return -1 + return -1; + } + + public static void intTester() { + int i; + int x; + // The sorted array we will search + int[] sortedArr = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}; + // A list of numbers not in the array, to see if our function correctly returns -1 + int[] extras = {12, -2, 34}; + + for (i = 0; i < sortedArr.length + extras.length; i += 1) { + if (i < sortedArr.length) { + x = sortedArr[i]; + } else { + x = extras[i - sortedArr.length]; + } + + System.out.print("Searching for element: " + x + "; "); + int result = binarySearchInt(sortedArr, x); + if (result != -1) { + System.out.println("Element found at index: " + result); + } else { + System.out.println("Element not found"); + } + } + } + + public static void main(String[] args) { + intTester(); + } +}