binary search in js

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

showing results for - "binary search in js"
Agustina
02 Feb 2019
1const arr = [1, 3, 5, 7, 8, 9];
2const binarySearch = (arr, x , start=0, end=arr.length) => {
3  // If the item does not exist, return -1
4  if(end < start) return -1;
5  
6  // Calculate middle index of the array
7  let mid = Math.floor((start + end) / 2);
8  
9  // Is the middle a match?
10  if(arr[mid] === x) return mid;
11  // Is the middle less than x
12  if(arr[mid] < x) return binarySearch(arr, x, mid+1, end);
13  // Else the middle is more than x
14  else return binarySearch(arr, x , start, mid-1);
15}
16
17binarySearch(arr, 7); // Returns 3
18/* O(n) of binarySearch is log(n) */
Corentin
21 Jul 2018
1function binarySearch(arr, val) {
2  let start = 0;
3  let end = arr.length - 1;
4
5  while (start <= end) {
6    let mid = Math.floor((start + end) / 2);
7
8    if (arr[mid] === val) {
9      return mid;
10    }
11
12    if (val < arr[mid]) {
13      end = mid - 1;
14    } else {
15      start = mid + 1;
16    }
17  }
18  return -1;
19}
20
Blanche
19 Mar 2020
1function binarySearchIndex (array, target, low = 0, high = array.length - 1) {
2  if (low > high) {
3    return -1
4  }
5  const midPoint = Math.floor((low + high) / 2)
6
7  if (target < array[midPoint]) {
8    return binarySearchIndex(array, target, low, midPoint - 1)
9  } else if (target > array[midPoint]) {
10    return binarySearchIndex(array, target, midPoint + 1, high)
11  } else {
12    return midPoint
13  }
14}
queries leading to this page
binary javascriptjs binary search 5cbinary search js examplebinary search treww javascriptjavascript binary search array jsbinary serch jshow to binary search an array in javascriptbinary search alogrithm javascriptbinary search code in javascriptbinary search program jsbinary function jswhat is a binary search in javascriptbinary search algorithm array jsbinary searches in javascriptjs binary search importsearch binary jsbinary search on array in javascriptimplementing binary search of an array javascriptgenerate binary numbers queue javascriptbinary search in jsbinary search solution jswhat is binary array search in jsdicotomic search javascriptrecursive binary search javascriptjs search middle number algorithmbinary search algorithm in javascriptbinary search js binary search string jsbinary search algorithm jsbianary search in jsbinary in jsbinary search algorithm with jsbinary search string javascriptbinary search in jscriptbinary saerch javascriptjavascript 2b binary searchbinary searcbh jshow to write binary search algorithm javascript 5djs find binary searchjavascript array binary search index of elementbinary search implementation jsbinary operation javascript w3schoolwrite a javascript program to perform a binary search javascript recursive binary search functionbinarysort kth javascriptis there a binary search function in javascripthow to work through a binary search problem javascriptbianry search javascriptbinary search return index javascriptjs binarysearchlinear search quiz jsjs problems in binary searchjavascript binary search algorithmjavascript binary search es6a number is present or not binary search javascriptbinary search javascript programmeeasy binary search jsbinary function javascriptjs binary search find for 1lower bound javascript binary searchimplement binary search in javascriptjavascript binary search algorithmarray search in sorted array javascriptbinart search in jsbinary search js arraybinary search search in javasriptjavascript binary search usesjavascript binary search on array binary search javascript w3schools for objectnode js binary searchjavascript binary search solutionjavascript binary search arrayhow to do binary search in javascriptbinary sort in javascript programjavascript binary searcghow to write binary search algorithm javascriptbinary search program in javascriptbinary search examples jsjavascript binary search whiledoes javascript set use binary searchhow to implement binary search in javascriptbinary search algorythim jssorting binary search in javascriptbinary search by name javascriptbinary search algoritm jsbinarysearch index javascripthow to solve binary search in jsbinary search js codejavascript binary search used for search formarray binary search javascript e2 80 9cjavascript binary searchjavascript binary search with only arraybinarysearch javascriptstandard binary search function javascriptjavascript binary searchjs array dichotomic searcharray javascript binary search methodbinary serch in jsjs binary search array of stringsjs binary search arraybinary search array jsjs binary search functionbinary search for array javascriptupper bound binary search javascriptbinary search js forwhat is binary search in javascriptbinary search javascript codejs binary serachfunction in javascript for binary searchbinary search explanation in javascriptbinary search jsbinary search in javascsriptjs binary search in arrayjavascript create binary treebinary search in javascript inbuilddoes javacript array 2cfind use binary searchsequence of adjacent keywords binary search javascriptany function for binary search in javascriptbinary search javascript w3schoolsbinary search javascript return arrayjavascript binary search on ascending orderjs binarysortuse of binary search in jsjs binary search algob search javascriptrecursive binary search algorithm jsbinary search jsbinary search implementation javascriptimplement the binary search algorithm javascripbinary search in javascriptbinary search algorithm javascriptjs 2bbinary search algorithmimplement binary search jsbinary search javascript templatebinary search in sorted array in javascriptbinary search array javascriptbinary sort jsdichotomic search javascriptbinary search algorithm javascript exampledo a binary search javascriptbinary search quiz jsbinary sort in javascriptbinary search in javascript arrayrecursive binary search jsimplement binary search in given array javascriptbinary search javasciptbinary search js solutionbinary search array javascriptjavascript binary search non recursivebinary search in javascriptwriting a binary search jsbinary search coding problems javascriptbinar search javascriptbinary search using jsjs binary search stringjavascript binary search number guessing gamebinary search javacriptcode example of binary search javascriptbinary representation javascriptbinary search nodejsbinary search in an array in js for searching elementsprogram for binary search js arrayjavascript binary search string arraynodejs binary search arraylinear search vs binary search javascriptbinary search vs binary search tree jsjavascript find element in array using binary searchbinary search ttree javascriptbinary representation in javascriptque es binary search in javascript binary search example step by step javascriptjavascript dichotomic searchbinary search code jsbinary search strings javascriptbinar search jsbinary search java scriptbinary search alorithm example in javascriptbinaryserachindex javascriptbinery search in jaavascriptbinary search javascritbinary representation jsbinary search program in jsjavascript divide array binary searchbinary search javascrpthow to binary search javascriptbinary search height javascriptalgorithm in javascript binary search explainedbinary search on a binary tree javascriptbinary search javscripthow to write a binary search jsbinary search algo using jsbinary search in array javascriptjavascript binary search code examplejavascript binary search iterativebinary search javascript for beginnersjavascript binary search libraryjs binary search librarybinary search code javascriptbinary search implementation javascriptbinary search in javascript node in binary searchbinary search array in javascriptjavascript built in binary searchbinary search with javascripthow to write a binary search function in javascriptjavascript binary search functionbinary search implementation in jsbinary search javascript examplesolving binary search in jsbinary search in sorted array jsbinary search on jsbinary search fuction bi index jswhat is a binary search algorithm javascriptbinary search node jsbinary value for javascriptbinarysearch jsbinary serach javascriptjavascript bineary searchbinary search in matrix in javascriptjs binary searchbinary search in havascripthow to do binary search jsnode js binary search implementationbinary search function jsbinary search javascriptjs binary and linear searchbitwise search array javascriptbinary search algorithm in javascript mediumbinary search using javascriptbinary search solution javascriptbinary search algorithim javascriptbinary search nodejs return arrayidentify a binary data in jsjs binary search custom functionjavascript binary search sorted arraybinary search small quiz jsbinarytosearch javascript explanationbinary search javasript formulasimple javascript binary searchbinary search function javascriptjs binary searchjs array binary searchbinary algorithm javascripthow to make a binary search in an array javascriptjavascript binarybinray search javascriptbinary search algorithm in jsbinary js searchhow to sort and binary search javascriptsearch by name javascript binarybinary sort javascriptwhat is a binary search in jsimplement a binary search algorithm using jsbinary seach jsjavascript array binary searchbinary search in javascrioptlinear search and binary search in javascriptbinary search in javascript stringjavascript binary search codebinary search in js