Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. //Modified Binary Search
  2. //Darren Mock
  3.  
  4. import java.util.*;
  5.  
  6. public class BinaryS{
  7. public static void main(String [] args){
  8. int[] A;
  9. A[0] = -14;
  10. A[1] = -10;
  11. A[2] = 2;
  12. A[3] = 108;
  13. A[4] = 108;
  14. A[5] = 243;
  15. A[6] = 285;
  16. A[7] = 285;
  17. A[8] = 285;
  18. A[9] = 401;
  19. }
  20. int binary(int[] A, int index){
  21. int index = A.length/2 - 1;
  22.  
  23. for (i = 0; i < Math.log(A.length + 1)/Math.log(2); i++) {
  24. if(A[index] > key){
  25. index = index - A.length/(Math.pow(2,(i + 1)));
  26. }
  27. else if(A[index] < key){
  28. index = index + A.length/(Math.pow(2,(i + 1)));
  29. }
  30. else if (A[index] == key){
  31. if (index != 0 && A[index - 1] == key){
  32. index = index - 1;
  33. }
  34. return index;
  35. }
  36. return -1;
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement