Advertisement
solokiller11

Ex14.2 - Lee

Dec 26th, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1.  
  2. /**
  3. * Write a description of class Ex14 here.
  4. *
  5. * @author (your name)
  6. * @version (a version number or a date)
  7. */
  8. public class Ex14
  9. {
  10. public static boolean search(int[][] mat, int num)
  11. {
  12. int rL = 0, rH = mat.length;
  13. int cL = 0, cH = mat.length;
  14. int pR = rL + (rH - rL)/2;
  15. int pC = cL + (cH - cL) / 2;
  16.  
  17. for(int i = 0; i < Math.log(mat.length)/Math.log(2); i++)
  18. {
  19. pR = rL + (rH - rL)/2;
  20. pC = cL + (cH - cL) / 2;
  21. if(num >= mat[pR][0])
  22. {
  23. rL = pR;
  24. cH = pC;
  25. }
  26. else if(num >= mat[pR][pC])
  27. {
  28. rL = pR;
  29. cL = pC;
  30. }
  31. else if(num >= mat[0][pC])
  32. {
  33. rH = pR;
  34. cL = pC;
  35. }
  36. else
  37. {
  38. rH = pR;
  39. cH = pC;
  40. }
  41. }
  42.  
  43. pR = rL + (rH - rL)/2;
  44. pC = cL + (cH - cL) / 2;
  45. return mat[pR][pC] == num;
  46. }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement