binary search in java

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

showing results for - "binary search in java"
Giuseppe
14 Oct 2020
1#include <iostream>
2using namespace std;
3
4// This program performs a binary search through an array, must be sorted to work
5int binarySearch(int array[], int size, int value) 
6{   
7    int first = 0,         // First array element       
8    last = size - 1,       // Last array element       
9    middle,                // Mid point of search       
10    position = -1;         // Position of search value   
11    bool found = false;        // Flag   
12    while (!found && first <= last) 
13    {      
14        middle = (first + last) / 2;     // Calculate mid point      
15        if (array[middle] == value)      // If value is found at mid      
16    	{         
17                found = true;         
18                position = middle;      
19        }      
20        else if (array[middle] > value)  // If value is in lower half         
21            last = middle - 1;      
22        else         
23            first = middle + 1;          // If value is in upper half   
24    }   
25    return position;
26}
27int main ()
28{
29    const int size = 5; // size initialization
30    int array[size] = {1, 2, 3, 4, 5}; // declare array of size 10
31    int value; // declare value to be searched for
32    int result; // declare variable that will be returned after binary search
33
34    cout << "What value would you like to search for? "; // prompt user to enter value
35    cin >> value;
36    result = binarySearch(array, size, value);
37
38    if (result == -1) // if value isn't found display this message
39        cout << "Not found\n";
40    else  // If value is found, displays message
41        cout << "Your value is in the array.\n"; 
42  
43    return 0;
44}
Dante
15 Oct 2019
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} 
Geoffrey
28 Jun 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); } } 
Emir
05 Jun 2019
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
Ophélie
03 Nov 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}
Javier
24 Oct 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}
queries leading to this page
binary search in java inbuiltalgorithm of binary search javawrite a program to search a key in a list using binary searchbinary search c 2b 2b programarrays binary search javabinary search in array c 2b 2b examplebinary search algortihmbinary search in jvabinary search array cppbinary search formula javabinary search code cppbinary search cpphow to code a binary search in javabinary search c 2b 2b stlbinary search using javabinary search java collectionsbinary search tree c 2b 2bbinary search c 2b 2bcodejava binary search commandbinary search without function in javahow to binary search in javabinary search for strings in javawrite algorithm for binary search javabinary search function in c 2b 2bimplement binary search inc 2b 2bbin search c 2b 2bbinary search java algorithmbinary search c 2b 2b codeimplementation binary search in javac 2b 2b algorithm binary searchbinary search to find 3e 3delemnetcode c 2b 2b exercises binary searchbinary search code gfg c 2b 2btechnicspub 7c c 2b 2b algorithm series binary search algorithm and recursionbinary searh in c 2b 2bc binary searchc 2b 2b binary search in arraybinary search algorithm in data structure c 2b 2b codewhat is a binary search in javabinary serach in cbinary search in array returns in javac 2b 2b example of a binary searchbinary search left in cbinary search javwrite a program for binary search in c 2b 2bbinary search of array in cbinary search 28 29 in c 2b 2bformula for binary search in arraydoes java have a built in binary searchgreeks for greeks binary searchalgorithmsbinary search examplehow to do a binary searchbinary search algorithm javaconstruct binary search tree using queue in c 2b 2bbinary search exampe c 2b 2bhow many arguments do you need for binary searchbinary search java c2 a8c 2b 2b binary search stdbinary search algorithm c 2b 2bbinary serch in javabinary search stimulation in cppbinary searching c 2b 2bjava binary search how to usebin search cbinary sebinary sarchbinary shearchjava binarysearchbinary search simple java codebinaryy search program in c 2b 2bbinary search c 2b 2b stringc function for binary searchbinary search in jvaabinary search in c 2b 2bhow to use binary search javaiterative binary search in c 2b 2bbinary serach uign javahow to do binary search in javabinary search tree c 2b 2b codebinary search 28 29 in javaarrays binarysearch in javajava binary search builtinbinary search in c 5cbinary search using cbinary search tree javacpp binary search functionbinary search in c 2b 2b stllinear and binary search javawrite a binary search function binarysearcharray for an array of ordered list of integers binary search c 2b 2b algorithmbinary search loo 5b c 2b 2bmost simple code in binary search in javabinary search function in javabinary search javasbinary search string array javabinary search c 2b 2b iterativeprogram to implement binary search in javahow to binary search an array in javabinary search builtin javac code for binary searchbinary sort in javahow to implement binary search in javahow to use a binary search in javajava simple binary searchbinary search in javvabinary search in c 2b 2b geeksforgeeksis there a binary serach function in c 2b 2bbinary seach in c 2b 2bbinary search inbuilt function in javajava binary searchbinary search in java using functionbinary tree search javabinary search function c 2b 2bbinary search pythonnbinary search for beginners in javabinary search struct c 2b 2bbinary search in a binary arraybinary search with 2 middle elementhow to write a binary search javabinary search tree programs cpphow to do a binary search in javajava binary search librarybinary search in cpp programbinary seacrch in avajava program to perform binary searchhow to use binary search function in c 2b 2bbineary search codebinary search in codingbinary search jsvac 23 binary searchbinary search in an arrayhow to make a binary search algorithm c 2b 2bbinary search sort program in c 2b 2bbinary serach in javabuilt in binary search c 2b 2bbinary search in array program in c 2b 2bhow does binary search an array javabinary search function javaimplement binary search in javawhat does binary search return javabinary search java inbuiltcpp stl binary searchsearch an element using binary search in javac 2b 2b bimary search recursivebinary search in recursion c 2b 2bbinary search in c 2b 2b iterativec bnary searchbinary search on strings javahow to call a binary search into a main class method in javabinary search with searchbinary search algorithm code in javasolution mid term binary search c 2b 2bbinary search code in javabinary search c 2b 2b stlbinary search in java built inarray binary searchcode binary search examplebinary search implementation c 2b 2busing binary search in javabinary search array cppphow to perform binary search in javabinary search algorithm in c 2b 2b programwhat does binary search return in c 2b 2bbinary search in java implementedbinary search inbuilt function in c 2b 2bbinary search in array java collectionsbinary searchingbinary search and javabinary search java examplearrays binary search function in jvaarrays binarysearch codebinary search java documentationbinary search by user input using cppbinary search simple way c 2b 2bbinary searchin c 2b 2bbinary search class in javabinary search in c implement binary search javabinary search cpp codebinary search stl in c 2b 2bbinary searchjavabinary search c 2b 2b examplebinary search java programbinary search tree c 2b 2b implementationexam exercise binary search c 2b 2b binaryseach javabinary serarch c 2b 2bbinary search in c programbinary search implementation using java collectionsc 2b 2b binary search methodbinary search java arraybinary search programbinary search inbuilt function in cppbinary search algorithm for cppbinary search implementation in javajava binary search string arrayjava program to search the string using binary searchjava binary search grepeerbinary search program in c 2b 2b stlwhat is the binary search algorithm javabinary search inside binary searchwrite the code for binary search javac 2b 2b binary search functuonbinary search snippet cppwrite a program for binary search in javabinary and linear search program in c 2b 2bbinary search for given arraybinary seacrch javabinary search cpp stlcpp binary searchbinary searching technicbest way to implement a binary search in javabinary search for javabinarysearch in cc 2b 2b binary searchwhat is binary search in c 2b 2bbinary search java codebinaray search in the arraryhow to perform binary search javabinary search tjavabinary search jaavjava collection binary searchbinary search finction in c 2b 2bbinary search in array in javac program to find complexity of binary searchbinary searching in c 2b 2bjava arrays binarysearch c 2b 2b binary searchhow to code binary search in javac 2b 2b binary search functionarray binary search in javvabinary search c 2b 2b gfgbinary search in arraybinary serach implementationbinary search in jva abinary search java built inbinary search int array javajava in buld binary searchbineary searchbinary search problem javac 2b 2b binary search function formatbinary search 28recursive 29binary search algorithmin c 2b 2bwrite a program to perform binary search for a number in javabinary search of an array in javabinary serach codec 2b 2b recursive binary searchimplement the binary search algorithmbinary search array jababinary search function c 2b 2bbinary search in java programsearch an element in an array using binary searchbinarysearrch java binarysearch javabinay search cbianry searchbinary search for integersbinarysearchbinary search cpp builtin binary search java implementationbinary search cpp gfgc 2b 2b binary search int arrayimplementation of binary searchbinary search to find an element in array java binary search arraybinary searching in javabinary search program in cppcode for binary sort javajava code for binary searchbinary search java 9java how does binary search an arrayjava binary searchbinary search list in jvahow to use binary search in java collectionsbinary search java utilbinary serach javajava binary search codebinary search library javabinary search with algorithm c 2b 2bbinary search famous program in c 2b 2bbinary search algorithm in c 2b 2bbinary search java 8 examplebinary search for elementbinary search program in c 2b 2bbinary serach in c 2b 2bbinarysearch i javabinay searchbinary searcy c 2b 2bbinary search in java examplebinary search iterative in cppbinary search algorithm in javabinary search method in java binary search algorithm ccollection binary search javarecursive binary search in c 2b 2bbinary search for decresing array in c 2b 2bcpp binary search algorithmarrays binarysearch javajava array binary search methodbinary search algorithm c 2b 2bmethod for binary search in javac programming binary search1 write a program to search the elements of an array using binary search 3 4 in javabinary search in cpp stlbinary search in java collectionshow to use binary search in javaarray binary search javabinary search examplesjava program for binary searchimplement binary search in c 2b 2bjava binary search coderbinary searchc 2b 2bbinary search java intbinary search pseudocode c 2b 2bjava binary search binary search javabinary search code example in javabinary sort in java geeks for geeksbinary search of a number in c 2b 2bhow to use binary search in an array using javawhat is binary searchimplement bimary search how to code binary searxch through oopshow does binary search in c 2b 2bcode binary searchbinary serach algorithmbinary search c 2b 2b function 3bbinarysearch in javabinary serach java codebinary search string javabinary using arrayimplementing binary searchbinary search c 2b 2brecursive function for binary search in cppc 2b 2b program to binary searchto search element using binary searchexplain the binary search process java java binary searchprogram of binary search in c 2b 2bbinary search method java examplejava binary search programbinary search in c 2b 2b programwrite a program to implement binary searchhow to binary search in cppbinarysearch by 3 codebinary search arry javabinary search using c 2b 2bbinary search c 2b 2b functionbinary search java examplesbinary search program using divide and conquer in chow to do binary serch in javabinary search string javawrite a program for binary searchbinary search in java 8bindary search in chow to binary search func in cppbinary search c 2bbinary search std c 2b 2bbinary search c 2b 2b algorithmbinary search java methodbinary search index javaarrays java binary searchbinary search program in c 2b 2b using classbinary search algorith in javawhat is binary search for begginers in javabinarysearch code javabinary search c 2b 2bbinary search implementation of java collectionsbinary search algorithm example in javacode for binary search in cbinary search recursivebinary search in 2b 2bwhat is binary search c 2b 2bbinary search c 2b 2b sbinary sreach in cbinary search program in javabinary search implementation javabinary search using stl c 2b 2bbinary search in array javabinary search function cpp logic of binary search with position if element not foundbinary search gfg javac 2b 2b binary search iterativebinary search api in javaprebuilt binary search implementation in javabinary seach javajava binary search methodbinary search example javac 2b 2b utility binary searchhow to do binary serach in c 2b 2bbinary search algorithmbinary search for array in javabinary search code javabinary search functionmost efficient binary search code in javalinear search in java using recursionbinary search using two dependenciesbinary search java 8binary search function in c implementationbinary search function program in cbinary search array of strings c 2b 2bbinary search parameters in javabinary search in cppbinary search using recursion cppbinary string search c 2b 2bhow to implement binary search javajava string binary searchbinary search in java without function java binary search examplecode to find element in the binary search tree c 2b 2bjava binary search exampea 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 of an array cppbinary search arraybinary search java definitionbinary search string java examplesearch element using binary searchbinary search in c 2b 2bimplementing a binary search in javabinary searching c 2b 2bsyntax of binary search in cppbinary search tree program in c 2b 2bwhat is binary search java simple binary search javapyhton c code for binary searchbest binary search javabinary search in javabinary search function cppbinary search cppbinary search code in c 2b 2bbinary search cde in c 2b 2bbinary sort in c 2b 2bsearchbinary 28 29 on javabinary search on javabinary search simple javabinary search programiz in c 2b 2bjava implementation of binary searchbinarysearch java examplebinary search sort java methodbinary searc cbinary search array in cimplement binary search in cformula for binary search in javahow binary search search works in javabinary search program c 2b 2bbinary searching javatime order of binary search in cppbinary search logic in javabinary search tree in javabinary search method javabinary search java docsbinary search example in javabinary search stl in c 2b 2bbinary search java 8 returnuse 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 2b 2bbinary search array avabinaru search implementationbinary search integer array program javac 2b 2b program for binary search in an arraybinary search inbuilt c 2b 2bc program binary searchbinary search in java algorithmbinary search java runtimewrite a program to search an element x in an array of n integers using binary search algorithm that uses divide and conquer technique binary search using cppjava program that demonstrates the use of 3a binary searchbinary search implementation of java collection c 2b 2b binary search stlset binary search in javabinary search for arrayc 2b 2b program to implement binary searchbinary search explanation in javabuiltin binary search javabinary search in java api binary search in algorith in c 2b 2bbinary search element in c 2b 2bfind by binary search c 2b 2bbinary search string in javajava binary search method collectionsjava binary search for inthow to do a binary search in c 2b 2bbinary search java arraysbinary search code in cppbinary search examples javabinary search generic javabinary search tree string javabinary search in c 2b 2b stlwrite a program to implement binary search algorithm in javacode java binary searchbinary search javavbinary search examle javabinary search by user in javabinary search function stl in c 2b 2bbinary searcbinary search program using divide and conquerbinary search in array c 2b 2bbinary serach code in javahow does binary search work in java with examplebin search recursively c 2b 2bhow to display binary searchingbinary search algorithm pythoncoding binary searchbinarysearch java methodbinary seach codejava array binary searchbinaru searchnew solution 28 29 binarysearch 28mat 5bi 5d 29 3bbinary search in javbin search javac 2b 2b built in binary searchhow does binary search work in javabinary serch function in javajava program of binary search using arraybinary search algorithm c 2b 2b codebinary search ree javacall binary searchbinary search in java geeksforgeeksbinary search in stl c 2b 2busing iterative binary search in a survey in c 2b 2bimplement a binary searh in javabinary search application i cppalgorithm of binary search javabinary search in c 2b 2b arraysjava binary search stringbinary search c 2b 2b coidejava binary search source codeimplement binary searchjava binary search algorithmbinary search codes c 2b 2bbinary searchh cppbinary search algorithm havabinary search code for c 2b 3dbinary search implementation in c 2b 2bbinary search algorithm c 2b 2bbinary search in c 2b 2b arraywrite a binary search algorithm javawrite a program for binary search algorithm in javac 2b 2b algorithm std binary searchprogram for binary search in cbinary search function for int array in javabinary search implementions c 2b 2bcreate binary search tree program in c 2b 2bbinary serach cppbinary search c 2b 2b 5cwrite a java program to search for an element in a given list of elements using binary search mechanism c 2b 2b code for binary searchbinary search java 27binary search in java gfgjava binarysearch examplebinary search sorted array javahow to display binary searching c 2b 2bjava binary search implementationjava binary search built inbinary find javahow to call a binary search into a main class in javacode for binary search in c 2b 2bbinear search in c 2b 2bbinary search using divide and conquer c 2b 2bbinary search geeksforgeeksbinary search function c 2b 2b array with whilesorted 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 foundbinary search hjavac 2b 2b function to do binary search in an arrayarray binary search algorithm c 2b 2bjava binary search representationc 2b 2b binary search in mainbinary search program in cbinary search c 2b 2b cppreferencejava binary search for all elementsbinary search javabinary search cbinary search code c 2b 2bcollections binary search in javabinary search class 12th c 2b 2bcpp program for binary searchhow to write a binary search in javabinary search stl c 2b 2bbinary search in java o 28n 29binary seach c 2b 2bbinary search library in c 2b 2bbinary search in pc 2b 2b program for binary search in an array using sortingbinary search in javavstring binary search in cpparray binary search in javabinary search c 2b 2b for answerimplement binary search tree for strings in c 2b 2bhow to perform binary search in jhavabinary search kavabinary search array javabinary search javabin search in cppbinary search on the array in c 2b 2bbinary search manually in javabinary search in c 2b 2b with arrayc 2b 2b binary search tree searchbinary search in java sortedbinary search in java util exampleimplement binary search in javabest algorithm for binary search in cbinary search c programbinary search using c 2b 2b thumbnail pngbinary search in c 2b 2bbinary search in cppbinary searhc javabinary search sort javahow to write binary search in javahow to implement binary search into a main class in javaarrays binarysearchbinary seaarch program in c 2b 2bbinary search in c 2b 2b codejava arrays binary searchbinary search function in c 2b 2b stlbinarysearch implementationbinarysearch method javawhat is binary search in javabinary search for integer in javabinary search algorithms javajva binary searchbinary search ajvabinary seachc 2b 2b binary search algorithmbinary search array c 2b 2bbinary searhc in javabinary search sort function javabinary search built in function javabinary search in c 2f 2b 2bbinary search codebinarysearch javacode for binary searchbinary search for string in javabinary searchstring javabinary search javadocbinary search c 2b 2b for a numberbinary tree java searchbinary search search count value javabest java programs fbinary search in matrix in javabinary search program javacpp binary search treebinary search in java arraysc 2b 2b binary search main binary search in java