Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Search {
- public static void main( String args[] ) {
- long key = 5;
- long search[] = {1,2, 2,2,5,5,5,5,5, 5,5,5,9,9,9,9};
- int item = binarySearch(search, key);
- for
- System.out.println( );
- }
- public static int binarySearch(long[] a, long key) {
- int bot = -1;
- int top = a.length;
- int counter = 0;
- while (top - bot > 1) {
- int mid = bot + (top - bot) / 2;
- if (key > a[mid]){
- bot = mid
- }
- else{
- top = mid;
- }
- if (a[top] == key){
- return top;
- }
- else{
- return -top - 1;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment