Function to perform binary search on the mid values of row to get the desired pair of rows where the element can be found.
49{
50 if (n == 1)
51 {
53 return;
54 }
55
56
57
58 int i_low = 0, i_high = n - 1, j_mid = m / 2;
59 while ((i_low + 1) < i_high)
60 {
61 int i_mid = (i_low + i_high) / 2;
62
63 if (mat[i_mid][j_mid] == x)
64 {
65 printf("Found at (%d,%d)\n", i_mid, j_mid);
66 return;
67 }
68 else if (mat[i_mid][j_mid] > x)
69 i_high = i_mid;
70 else
71 i_low = i_mid;
72 }
73
74 if (mat[i_low][j_mid] == x)
75 printf("Found at (%d,%d)\n", i_low, j_mid);
76 else if (mat[i_low + 1][j_mid] == x)
77 printf("Found at (%d,%d)\n", i_low + 1, j_mid);
78
79
80 else if (x <= mat[i_low][j_mid - 1])
82
83
84 else if (x >= mat[i_low][j_mid + 1] && x <= mat[i_low][m - 1])
86
87
88 else if (x <= mat[i_low + 1][j_mid - 1])
90
91
92 else
94}
int binarySearch(const int **mat, int i, int j_low, int j_high, int x)
This function does Binary search for x in i-th row from j_low to j_high.
Definition modified_binary_search.c:18