Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First occurrence in a binary search

I'm tinkering with some code and I realized something I never knew. A normal binary search will return a random index in a data set for a key that occurs more than once. How can I modify this code below to return the first occurrence? Is this something people do?

//ripped from the JDK public static int binarySearchValue(InvertedContainer.InvertedIndex[] a, long key) {     return bSearchVal(a, 0, a.length, key); }  private static int bSearchVal(InvertedContainer.InvertedIndex[] a, int fromIndex,                                  int toIndex, long key) {     int low = fromIndex;     int high = toIndex - 1;      while (low <= high) {         int mid = (low + high) >>> 1;         long midVal = a[mid].val;          if (midVal < key)             low = mid + 1;         else if (midVal > key)             high = mid - 1;         else             return mid; // key found     }     return (low); // key not found. return insertion point } 
like image 968
Amir Afghani Avatar asked Jul 13 '11 08:07

Amir Afghani


People also ask

Which element is found in first iteration of binary search?

Binary Search is performed on a sorted array. For each iteration, 1. It first finds the mid element's value.

Which method is used to get the first occurrence of specified element?

indexOf(Object o) Method: This method is used to get the index of the first occurrence of the specified element in the vector.


1 Answers

An addition to Jon Skeets post:

The potential faster implementation is actually not hard to implement and adds only 2 lines of code, here is how I'd do it:

    if (midVal < key)         low = mid + 1;     else if (midVal > key)         high = mid - 1;     else if (low != mid) //Equal but range is not fully scanned         high = mid; //Set upper bound to current number and rescan     else //Equal and full range is scanned         return mid; 
like image 143
bezmax Avatar answered Sep 22 '22 02:09

bezmax