Binary search, implemented, as always, by taking a [peek at Sedgewick](http://algs4.cs.princeton.edu/11model/BinarySearch.java.html).
168576
function binarySearch (comparator, array, key) {
  var low = 0, high = array.length - 1, partition, compare;
  while (low <= high) {
    partition = Math.floor(low + (high - low) / 2);
    compare = comparator(key, array[partition]);
    if (compare < 0) high = partition - 1;
    else if (compare > 0) low = partition + 1;
    else return partition;
  }
  return low;
}
114072
private static <T> int binarySearch0(Sortable<T> a, int fromIndex, int toIndex, T key, Comparator<? super T> c) {
        if (c == null) {
            throw new NullPointerException();
        }
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            T midVal = a.at(mid);
            int cmp = c.compare(midVal, key);
            if (cmp < 0)
                low = mid + 1;
            else if (cmp > 0)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1); // key not found.
    }
1191517
public List<Integer> searchBigBytes(byte[] srcBytes, byte[] searchBytes, int numOfThreads) {

		if (numOfThreads == 0) {
			numOfThreads = 1;
		}

		final int sizeOfSrcBytes = srcBytes.length;
		final int sizeOfSearchBytes = searchBytes.length;

		final int bytesToReadBlockSize = (srcBytes.length - (sizeOfSearchBytes)) / numOfThreads;

		final int threadPoolSize;

		if (maxNumOfThreads == THREADS_NO_LIMIT) {
			threadPoolSize = numOfThreads;
		} else {
			threadPoolSize = maxNumOfThreads;
		}

		final ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize);

		final List<Future<List<Integer>>> futureList = new ArrayList<Future<List<Integer>>>();

		for (int i = 0; i < numOfThreads; i++) {

			final int offset = bytesToReadBlockSize * i;
			final int readLeng;

			if (i == numOfThreads - 1) {
				// if it's the last element.
				readLeng = sizeOfSrcBytes - offset - 1;
			} else {
				// else , add the overlapping part size to blockSize
				readLeng = bytesToReadBlockSize + sizeOfSearchBytes;
			}

			final Future<List<Integer>> future = executorService.submit(new BinarySearchTask(srcBytes, searchBytes, offset, readLeng));

			futureList.add(future);
		}
		executorService.shutdown();

		// Remove duplicate indexes
		final List<Integer> resultIndexList = new CopyOnWriteArrayList<Integer>();

		for (Future<List<Integer>> future : futureList) {

			try {
				List<Integer> rawIndexList = future.get();

				for (int i = 0; i < rawIndexList.size(); i++) {

					Integer integer = rawIndexList.get(i);

					if (resultIndexList.contains(integer)) {
						// if already exists , skip
					} else {
						resultIndexList.add(integer);
					}
				}
			} catch (InterruptedException e) {
			} catch (ExecutionException e) {
			}

		}

		// Sort in ascending order
		resultIndexList.sort(new Comparator<Integer>() {

			public int compare(Integer num1, Integer num2) {
				if (num1 > num2) {
					return 1;
				} else if (num1 < num2) {
					return -1;
				}
				return 0;
			}
		});

		return resultIndexList;
	}
921897
private static int binarySearch(int[] a, int start, int end, int key) {
        int lo = start, hi = end-1; // search range is [lo,hi]

        // invariant lo<=hi
        while (lo <= hi) {
            int pivot = (lo + hi)/2;
            int v = a[pivot];

            if (v < key)        // needs to search upper half
                lo = pivot+1;
            else if (v > key)   // needs to search lower half
                hi = pivot-1;
            else    // eureka!
                return pivot;
        }
        return -(lo + 1); // insertion point
    }
966992
private int binaryGreaterSearch() {

        int low     = 0;
        int high    = count;
        int mid     = 0;
        int compare = 0;

        while (low < high) {
            mid     = (low + high) / 2;
            compare = compare(mid);

            if (compare < 0) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }

        return low == count ? -1
                            : low;
    }
1139058
function binarySearch(arr, item, comparator) {
  var low = 0;
  var high = arr.length;
  var mid;
  while (low < high) {
    mid = (low + high) >>> 1;
    if (comparator(arr[mid], item) < 0) {
      low = mid + 1;
    } else {
      high = mid;
    }
  }
  return low;
}
1268869
@Override
  public boolean containsKey(Object o) {
    @SuppressWarnings("unchecked")
    final K key = (K) o;
    return binarySearchInArray(key) >= 0 || overflowEntries.containsKey(key);
  }
709980
protected static int binarySearchGuess(@NotNull final BasePage page, @NotNull final ByteIterable key) {
        int index = binarySearchGuessUnsafe(page, key);
        if (index < 0) index = 0;
        return index;
    }
173272
def binarySearch(self, ip):
        
        if not ip.isdigit(): ip = self.ip2Long(ip)

        if self.__indexLen < 1:
            self.__f.seek(0)
            b = self.__f.read(8)
            self.__sPtr = self.getLong(b, 0)
            endPtr      = self.getLong(b, 4)
            self.__indexLen = endPtr - self.__sPtr

        startPtr = self.__sPtr
        indexLen = self.__indexLen

        self.__f.seek(startPtr)
        b = self.__f.read(indexLen+12)

        l, h, mixPtr = (0, int(indexLen/12), 0)
        while l <= h:
            m   = int((l+h)/2)
            ptr = startPtr + m*12
            self.__f.seek(ptr)

            b   = self.__f.read(12)
            sip = self.getLong(b, 0)
            eip = self.getLong(b, 4)

            if ip >= sip:
                if ip > eip:
                    l = m + 1
                else:
                    mixPtr = self.getLong(b, 8)
                    break;
            else:
                h = m - 1

        if mixPtr == 0: return "N2"

        return self.returnData(mixPtr)
990462
private V getBest(int t, byte[] b, int offset, int len) {
        for (int i = 0; i < len; i++) {
            byte c = b[offset + i];
            int index = __lookup[c & 0x7f];
            if (index >= 0) {
                int idx = t * ROW_SIZE + index;
                int nt = _rowIndex[idx];
                if (nt == 0)
                    break;
                t = nt;
            } else {
                char[] big = _bigIndex == null ? null : _bigIndex[t];
                if (big == null)
                    return null;
                int nt = big[c];
                if (nt == 0)
                    break;
                t = nt;
            }

            // Is the next Trie is a match
            if (_key[t] != null) {
                // Recurse so we can remember this possibility
                V best = getBest(t, b, offset + i + 1, len - i - 1);
                if (best != null)
                    return best;
                break;
            }
        }
        return (V) _value[t];
    }
1277191
void createBinaryTree()
    {
        int[] point = new int[VocabWord.MAX_CODE_LENGTH];
        char[] code = new char[VocabWord.MAX_CODE_LENGTH];
        int[] count = new int[vocabSize * 2 + 1];
        char[] binary = new char[vocabSize * 2 + 1];
        int[] parentNode = new int[vocabSize * 2 + 1];

        for (int i = 0; i < vocabSize; i++)
            count[i] = vocab[i].cn;
        for (int i = vocabSize; i < vocabSize * 2; i++)
            count[i] = Integer.MAX_VALUE;
        int pos1 = vocabSize - 1;
        int pos2 = vocabSize;
        // Following algorithm constructs the Huffman tree by adding one node at a time
        int min1i, min2i;
        for (int i = 0; i < vocabSize - 1; i++)
        {
            // First, find two smallest nodes 'min1, min2'
            if (pos1 >= 0)
            {
                if (count[pos1] < count[pos2])
                {
                    min1i = pos1;
                    pos1--;
                }
                else
                {
                    min1i = pos2;
                    pos2++;
                }
            }
            else
            {
                min1i = pos2;
                pos2++;
            }
            if (pos1 >= 0)
            {
                if (count[pos1] < count[pos2])
                {
                    min2i = pos1;
                    pos1--;
                }
                else
                {
                    min2i = pos2;
                    pos2++;
                }
            }
            else
            {
                min2i = pos2;
                pos2++;
            }
            count[vocabSize + i] = count[min1i] + count[min2i];
            parentNode[min1i] = vocabSize + i;
            parentNode[min2i] = vocabSize + i;
            binary[min2i] = 1;
        }
        // Now assign binary code to each vocabulary word
        for (int j = 0; j < vocabSize; j++)
        {
            int k = j;
            int i = 0;
            while (true)
            {
                code[i] = binary[k];
                point[i] = k;
                i++;
                k = parentNode[k];
                if (k == vocabSize * 2 - 2) break;
            }
            vocab[j].codelen = i;
            vocab[j].point[0] = vocabSize - 2;
            for (k = 0; k < i; k++)
            {
                vocab[j].code[i - k - 1] = code[k];
                vocab[j].point[i - k] = point[k] - vocabSize;
            }
        }
    }
518379
public static <T> int binarySearch(final T[] a, final T key, final Comparator<? super T> cmp) {
        return Array.binarySearch(a, key, cmp);
    }
1210348
private void search(double[] q, Node node, Neighbor<double[], E> neighbor) {
        if (node.isLeaf()) {
            // look at all the instances in this leaf
            for (int idx = node.index; idx < node.index + node.count; idx++) {
                if (q == keys[index[idx]] && identicalExcluded) {
                    continue;
                }

                double distance = Math.squaredDistance(q, keys[index[idx]]);
                if (distance < neighbor.distance) {
                    neighbor.key = keys[index[idx]];
                    neighbor.value = data[index[idx]];
                    neighbor.index = index[idx];
                    neighbor.distance = distance;
                }
            }
        } else {
            Node nearer, further;
            double diff = q[node.split] - node.cutoff;
            if (diff < 0) {
                nearer = node.lower;
                further = node.upper;
            } else {
                nearer = node.upper;
                further = node.lower;
            }

            search(q, nearer, neighbor);

            // now look in further half
            if (neighbor.distance >= diff * diff) {
                search(q, further, neighbor);
            }
        }
    }
185240
function binarySearch(arr, pred) {
    let begin   = -1;
    let end     = arr.length;

    while (1 + begin < end) {
        const mi = begin + ((end - begin) >> 1);

        if (pred(arr[mi]))
            end = mi;
        else
            begin = mi;
    }

    return end;
}
712659
def discrete_binary_search(tab, lo, hi):
    
    while lo < hi:
        mid = lo + (hi - lo) // 2
        if tab[mid]:
            hi = mid
        else:
            lo = mid + 1
    return lo
1238883
static int binarySearch(long keys[], int start, int len, long key) {
        int high = start + len, low = start - 1, guess;
        while (high - low > 1) {
            // use >>> for average or we could get an integer overflow.
            guess = (high + low) >>> 1;
            long guessedKey = keys[guess];
            if (guessedKey < key) {
                low = guess;
            } else {
                high = guess;
            }
        }

        if (high == start + len) {
            return ~(start + len);
        }

        long highKey = keys[high];
        if (highKey == key) {
            return high;
        } else {
            return ~high;
        }
    }
40673
private int binarySearchNonUnique(int fromIndex, int toIndex, long key1, long key2) {
		int low = fromIndex;
		int high = toIndex - 1;

		while (low <= high) {
			int mid = (low + high) >>> 1;
			long midVal1 = keys[mid];
			long midVal2 = values[mid];

        	if (midVal1 < key1) {
        		low = mid + 1;
        	} else if (midVal1 > key1) {
        		high = mid - 1;
        	} else {
            	if (midVal2 < key2) {
            		low = mid + 1;
            	} else if (midVal2 > key2) {
            		high = mid - 1;
            	} else {
            		return mid; // key found
            	}
        	}
		}
		return -(low + 1);  // key not found.
	}