binary search java

Solutions on MaxInterview for binary search java by the best coders in the world

showing results for - "binary search java"
Leona
22 Sep 2016
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} 
Laura
13 Jul 2016
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); } } 
Marcy
23 Nov 2020
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
Daniele
13 Sep 2018
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}
Renata
21 Mar 2019
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}
Catalina
20 Feb 2019
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.
queries leading to this page
binary serch in javabinary search in javformula for binary search in javahow does binary search work in java with examplebinary search with 2 middle elementbinary search method in java java simple binary searchbinary search algorithm in javabinary search implementation using java collectionsbinary sort in java geeks for geeksbinary search implementation of java collection binary search tree javawrite a program for binary search algorithm in javabinary search ajvajava binarysearchbinary search java utilbinary search cbinary search algorithms javabinary search in java algorithmbinary search formula javaformula for binary search in arraybinary search arry javabinary search in javvabinary search java programusing binary search in javawrite a java program to search for an element in a given list of elements using binary search mechanism binary search in java util examplebinary search tree string javajava binary search arraybinary shearchmost simple code in binary search in javajava binary search methodbinary search tjavabinary search string array javabinary search algorithmc 23 binary searchhow to perform binary search in javabinary search library javajava arrays binary searchbinary search implementation javaimplementing binary searchjava binary search built inbinary search for javabinary searhc javabinary search program in c 2b 2bbinary serach java codebinary search 28 29 in javabinary search sorted array javabinary serach implementationbinary search in jvajava binary search method collectionsjava array binary searchbinary search program in javabinarysearrch javaarray binary searchhow to implement binary search in javaprebuilt binary search implementation in javajava binary search codehow to write a binary search in javabinary search in java implementedbinary search in javavbinary search for strings in javabinary search examle javajava binary search how to usea function binarysearch 28 29 that implements the binary search algorithm for non empty sorted array using recursion the function should take the arguments list 2c value 2c low 2c high and returns the location of the searched valuebinary search in array returns in javabinary search simple java codebinary searhc in javacollections binarysearch javabinary search by user in javahow many arguments do you need for binary searchbinary search example javaarrays java binary searchbinary search method javabinary search and javaarrays binarysearch in javabinary search geeksforgeekshow to write a binary search javajava program that demonstrates the use of 3a binary searchnew solution 28 29 binarysearch 28mat 5bi 5d 29 3bbinary search code in javabinary search in java o 28n 29binary search on strings javabinary search function in javaimplementing a binary search in javajava binary search exampebinary search in arraybinaray search in the arrarybinarysearchwrite a program to implement binary search algorithm in javabinary search manually in javabinary search builtin javabinary search sort java methodhow to code a binary search in javabinary search ree javabinary search javbinary search inbuilt function in javabest java programs fbinary search in matrix in javabinary search pseudocode c 2b 2bwrite algorithm for binary search javabinary search javasbinary find javabinary search algorithm code in javajava implementation of binary searchbinary search c 2b 2bbinary tree java searchcode java binary searchbinary search string javabinary search generic javahow does binary search work in javajava program to search the string using binary searchhow to do binary serch in javabinarysearch java exampleset binary search in javabinary search java inbuiltbinary search index javajava in buld binary searchsearchbinary 28 29 on javabinary seach javajava binary search for intjava binary search programbinary search jsvahow to perform binary search in jhavajava collections binary search exampleimplementation binary search in javaprogram to implement binary search in javabinary searchingjava binary search for all elementsjava binary search builtinbest binary search javajva binary searchjava array binary search methodcode binary searchhow to do a binary search in c 2b 2bbinary search recursivebinary searching in javaimplement a binary searh in javajava binary search string arraybinary search java 9binarysearch implementationsearch an element using binary search in javabinary searchstring javacollection binarysearch in javabinary search in java inbuiltbinary search in cpphow to implement binary search into a main class in javabinary search java implementationarrays binarysearch codebinary seacrch javajava program for binary searchbinarysearch method javabinary search in java built inlinear search in java using recursionbinary search integer array program javabinary search explanation in javajava binary search representationjava binary search implementationhow to use binary search in java collectionsbinary serach uign javalogic of binary search with position if element not foundbinary search 28recursive 29what is binary searchbinary searchjavahow binary search search works in javahow to code binary searxch through oopsjava program to perform binary searchimplement binary search javaarrays binary search function in jvajava binary search coderhow to call a binary search into a main class method in javahow to do a binary search in javabinary search java 8how does binary search an array javabinary search snippet cppbinary search in jvaabinary search in java examplebinary search stimulation in cppmethod for binary search in javabinary search program javawhat does binary search return javabinary search java 27java binary search listwhat is the binary search algorithm javalinear and binary search javabinary search hjavabinary search java 8 returnbinarysearch java methodcollection binary search javajava binary search libraryjava program of binary search using arraybinary search java c2 a8binary search example in javabinary serach in javabinary search function for int array in javasimple binary search javabinary search java built inbinary search javavbinary search in java collectionsuse a binary search algorithm to find the number in the given array it returns true as the result if the number is found else it returns false as a resultbinary search problem javawrite a program for binary search in javabinary search parameters in javahow to use a binary search in javabinary search examples javajava binary search commandjava binary search grepeerbinary search tree in javadoes java have a built in binary searchtime order of binary search in cppc bnary searchbinary search jaavbinary search for integersbinary search string javabinary search in array java collectionsbinary search in an arraybinary search for beginners in javawrite a program to perform binary search for a number in javabinary search string java examplebinary search java documentationbinary search of an array in javabinary using arraybinary search java 8 examplewrite a binary search algorithm javabinary searching javahow to binary search in javabinary search int array javaimplement bimary search binary search algorithm havabinary search in java without function how to code binary search in javabinary search codeimplement binary search in javaarrays binary search javajava code for binary searchbinarysearch by 3 codebinary search for arraybinary search in java api code for binary sort javawrite the code for binary search javabinary search array jababinary search in java sortedbinary search function javabinary search arraybinary search kavabinary search java algorithmhow to use binary search javaimplement binary searchhow to binary search an array in javabinary search sort javabinarysearch i javacode for binary searchbinary search java examplejava how does binary search an arraybinary search in java gfgbinaryseach javagreeks for greeks binary searchalgorithmsmost efficient binary search code in javahow to implement binary search javahow to call a binary search into a main class in javahow to perform binary search javabinary search java docsbinary search algorithm pythonbinary search sort function javabinary serach codearray binary search in javvabinary search search count value javabinary search in java 8binary search code javabinarysearch javajava binary search stringbinary search in jva abinary search gfg javajava binary search examplebinary sort in javabinary search algorithm javahow to do a binary searchjava arrays binarysearchalgorithm of binary search javawhat is binary search java java string binary searchjava collection binary searchbinary search in java arraysbinary search list in jva java binary searcharray binary search in javabinary search java runtimebinary search api in javajava binary search source codebinary search string in javabinary search simple javawhat is binary search for begginers in javacall binary searchbinary tree search javaarrays binarysearch javabinary seacrch in avabin search javabinary search algorithm example in javabinary search for elementbinary search in javabinary search for given arraybinary search javabinary search javadocbinary search in java geeksforgeeksbinary search algortihmbinary search code example in javajava binary searcharrays binarysearchhow to do binary search in javabest way to implement a binary search in javabinary search java collectionsbinary search in java using functionbinary serach code in javahow to use binary search in javabinary search for integer in javabinary search in array javasorted array of n elements are given 2c write a program using binary search to search a given element x in a 5b 5d input 3a no of elements 2c elements in sorted order and the element to searched in an array output 3a the location where the elemnet is foundimplement binary search in javahow to use binary search in an array using javabinary serach javabinary search array javaalgorithm of binary search javabinary searching techniccollections binary search javabinary search for string in javacollections binary search objects java binary search with list javajava binarysearch examplecollections binarysearch javabinary search implementation of java collectionsbinarysearch java collectionsimplement the binary search algorithmbinary search programjava binary search algorithmjava binary searchhow to write binary search in javawhat is a binary search in javabinary search javabinary search using javabinarysearch code javabinary search java arraybinary search logic in javajava collections binarysearchbinary search algorith in javabinary search on javabinary serch function in javabinary search in array in javawhat is binary search in javabinary search java methodcollections binary search in javabinary search c 2b 2b sbinary search java codebinary search java definitionbinary search method java examplebinary search for array in javajava binary search binary search java arraysbinarysearch in javabinary search in java programbinary search built in function javabinary search class in javabinary search in pbinaru search implementationbinary search java intbinary search javabuiltin binary search javabinary search java examples binarysearch javaarray binary search javaexplain the binary search process javabinary search without function in javabinary search using two dependenciesbinary search implementation in javabinary search array avabinary search java