Posted By

leazen on 06/29/06


Tagged

search


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

nex
bsteadman


binary search


 / Published in: Java
 

  1. static public int search(int [] array, int target)
  2. {
  3. int high = array.length, low = -1, probe;
  4. while (high - low > 1)
  5. {
  6. probe = (low + high) >>> 1;
  7. if (array[probe] < target)
  8. low = probe;
  9. else
  10. high = probe;
  11. }
  12. if (high == array.length || array[high] != target)
  13. return -1;
  14. else
  15. return high;
  16. }

Report this snippet  

You need to login to post a comment.