Guest User

Untitled

a guest
Aug 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. public class Search {
  2. public static void main( String args[] ) {
  3. long key = 5;
  4. long search[] = {1,2, 2,2,5,5,5,5,5, 5,5,5,9,9,9,9};
  5. int item = binarySearch(search, key);
  6. for
  7. System.out.println( );
  8.  
  9. }
  10.  
  11. public static int binarySearch(long[] a, long key) {
  12. int bot = -1;
  13. int top = a.length;
  14. int counter = 0;
  15. while (top - bot > 1) {
  16. int mid = bot + (top - bot) / 2;
  17. if (key > a[mid]){
  18. bot = mid
  19. }
  20. else{
  21. top = mid;
  22. }
  23.  
  24. if (a[top] == key){
  25. return top;
  26. }
  27. else{
  28. return -top - 1;
  29. }
  30. }
  31. }
  32. }
Add Comment
Please, Sign In to add comment