Advertisement
ahmad_zizo

2d search O(log n)

Mar 13th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. int mat[n][m];
  7. int t=n-1, b = 0, l = 0, r = m -1, flag = 0,num; //num is the we search for
  8.  
  9. while(t>=b && l <= r)
  10. {
  11. if(mat[t/2][r/2]<num)
  12. {
  13. break;
  14. }
  15. if(mat[t/2][r/2]==num)
  16. {
  17. printf("[%d %d]",t/2,r/2);
  18. flag = 1;
  19. break;
  20. }
  21. if(mat[t][r/2] == num)
  22. {
  23. printf("[%d %d]",t,r/2);
  24. flag = 1;
  25. break;
  26. }
  27. if(mat[t/2][r] == num)
  28. {
  29. printf("[%d %d]",t/2,r);
  30. flag = 1;
  31. break;
  32. }
  33. if(mat[t/2][0] == num)
  34. {
  35. printf("[%d %d]",t/2,0);
  36. flag = 1;
  37. break;
  38. }
  39. if(mat[0][r/2] == num)
  40. {
  41. printf("[%d %d]",0,r/2);
  42. flag = 1;
  43. break;
  44. }
  45. if(mat[t/2][r/2] > num) //first quarter
  46. {
  47. t=t/2;
  48. r=r/2;
  49. continue;
  50. }
  51. else if(a[t/2][0] > num) //second quarter
  52. {
  53. t=t/2;
  54. l=r/2;
  55. continue;
  56. }
  57. else if(a[0][r/2] > num) //third quarter
  58. {
  59. b=t/2;
  60. r=r/2;
  61. continue;
  62. }
  63. else if(a[0][r/2] > num) //fourth quarter
  64. {
  65. b=t/2;
  66. l=r/2;
  67. continue;
  68. }
  69. }
  70. if(!flag) printf("NOT FOUND");
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement