1// Java implementation of iterative Binary Search
2class BinarySearch {
3 // Returns index of x if it is present in arr[],
4 // else return -1
5 int binarySearch(int arr[], int x)
6 {
7 int l = 0, r = arr.length - 1;
8 while (l <= r) {
9 int m = l + (r - l) / 2;
10
11 // Check if x is present at mid
12 if (arr[m] == x)
13 return m;
14
15 // If x greater, ignore left half
16 if (arr[m] < x)
17 l = m + 1;
18
19 // If x is smaller, ignore right half
20 else
21 r = m - 1;
22 }
23
24 // if we reach here, then element was
25 // not present
26 return -1;
27 }
28
29 // Driver method to test above
30 public static void main(String args[])
31 {
32 BinarySearch ob = new BinarySearch();
33 int arr[] = { 2, 3, 4, 10, 40 };
34 int n = arr.length;
35 int x = 10;
36 int result = ob.binarySearch(arr, x);
37 if (result == -1)
38 System.out.println("Element not present");
39 else
40 System.out.println("Element found at "
41 + "index " + result);
42 }
43}
1public class Main{ public static void binarySearch(int arr[], int a, int b, int key){ int mid = (a + b)/2; while( a <= b ){ if ( arr[mid] < key ){ a = mid + 1; }else if ( arr[mid] == key ){ System.out.println("number is here " + mid); break; }else{ b = mid - 1; } mid = (a + b)/2; } if ( a > b ){ System.out.println("number not here"); } } public static void main(String args[]){ int arr[] = {10,20,30,40,50}; int key = 30; int b=arr.length-1; binarySearch(arr,0,b,key); } }
1public int runBinarySearchRecursively(
2 int[] sortedArray, int key, int low, int high) {
3 int middle = (low + high) / 2;
4
5 if (high < low) {
6 return -1;
7 }
8
9 if (key == sortedArray[middle]) {
10 return middle;
11 } else if (key < sortedArray[middle]) {
12 return runBinarySearchRecursively(
13 sortedArray, key, low, middle - 1);
14 } else {
15 return runBinarySearchRecursively(
16 sortedArray, key, middle + 1, high);
17 }
18}
19
1binary search program in java.
2public class BinarySearchExample
3{
4 public static void binarySearch(int[] arrNumbers, int start, int end, int keyElement)
5 {
6 int middle = (start + end) / 2;
7 while(start <= end)
8 {
9 if(arrNumbers[middle] < keyElement)
10 {
11 start = middle + 1;
12 }
13 else if(arrNumbers[middle] == keyElement)
14 {
15 System.out.println("Element found at index: " + middle);
16 break;
17 }
18 else
19 {
20 end = middle - 1;
21 }
22 middle = (start + end) / 2;
23 }
24 if(start > end)
25 {
26 System.out.println("Element not found!");
27 }
28 }
29 public static void main(String[] args)
30 {
31 int[] arrNumbers = {14,15,16,17,18};
32 int keyElement = 16;
33 int end = arrNumbers.length - 1;
34 binarySearch(arrNumbers, 0, end, keyElement);
35 }
36}
1import java.util.Scanner;
2
3// Binary Search in Java
4
5class Main {
6 int binarySearch(int array[], int element, int low, int high) {
7
8 // Repeat until the pointers low and high meet each other
9 while (low <= high) {
10
11 // get index of mid element
12 int mid = low + (high - low) / 2;
13
14 // if element to be searched is the mid element
15 if (array[mid] == element)
16 return mid;
17
18 // if element is less than mid element
19 // search only the left side of mid
20 if (array[mid] < element)
21 low = mid + 1;
22
23 // if element is greater than mid element
24 // search only the right side of mid
25 else
26 high = mid - 1;
27 }
28
29 return -1;
30 }
31
32 public static void main(String args[]) {
33
34 // create an object of Main class
35 Main obj = new Main();
36
37 // create a sorted array
38 int[] array = { 3, 4, 5, 6, 7, 8, 9 };
39 int n = array.length;
40
41 // get input from user for element to be searched
42 Scanner input = new Scanner(System.in);
43
44 System.out.println("Enter element to be searched:");
45
46 // element to be searched
47 int element = input.nextInt();
48 input.close();
49
50 // call the binary search method
51 // pass arguments: array, element, index of first and last element
52 int result = obj.binarySearch(array, element, 0, n - 1);
53 if (result == -1)
54 System.out.println("Not found");
55 else
56 System.out.println("Element found at index " + result);
57 }
58}
1// Returns index of key in sorted list sorted in
2// ascending order
3public static int binarySearch(List slist, T key)
4
5// Returns index of key in sorted list sorted in
6// order defined by Comparator c.
7public static int binarySearch(List slist, T key, Comparator c)
8
9If key is not present, the it returns "(-(insertion point) - 1)".
10The insertion point is defined as the point at which the key
11would be inserted into the list.