BinarySearch

Run Settings
LanguageJava
Language Version
Run Command
import java.util.Arrays; class Main { public static void main(String[] args) { int[] array = new int[]{1,2,3,4,5}; Arrays.sort(array); // System.out.println("Hello World!:" + findFirstIndexOf(12, array)); System.out.println(Arrays.toString(array)); System.out.println("Hello World!:" + binarySearch(100, array)); } public static int binarySearch(int el, int[] arr) { return binarySearchRecursive(el, arr, 0, arr.length); } private static int binarySearchRecursive(int el, int[] arr, int start, int end) { if (arr == null || arr.length == 0 || start >= end-1) return -1; int mid = (start == 0) ? (end-start)/2 : (end-start)/2 + start; if (arr[mid] == el) return mid; else if (el < arr[mid]) { return binarySearchRecursive(el, arr, start, mid); } else { return binarySearchRecursive(el, arr, mid, end); } } }
Editor Settings
Theme
Key bindings
Full width
Lines