Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
modified_binary_search.c File Reference

Modified binary search algorithm More...

#include <stdio.h>
#include <stdlib.h>
Include dependency graph for modified_binary_search.c:

Functions

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. More...
 
void modifiedBinarySearch (const int **mat, int n, int m, int x)
 Function to perform binary search on the mid values of row to get the desired pair of rows where the element can be found. More...
 
int main ()
 Main function.
 

Detailed Description

Function Documentation

◆ binarySearch()

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.

Parameters
mat2D matrix to search within
irow to search in
j_lowstart column index
j_highend column index
xvalue to search for
Returns
column where x was found
-1 if value not found
19 {
20  while (j_low <= j_high)
21  {
22  int j_mid = (j_low + j_high) / 2;
23 
24  // Element found
25  if (mat[i][j_mid] == x)
26  {
27  printf("Found at (%d,%d)\n", i, j_mid);
28  return j_mid;
29  }
30  else if (mat[i][j_mid] > x)
31  j_high = j_mid - 1;
32  else
33  j_low = j_mid + 1;
34  }
35 
36  // element not found
37  printf("element not found\n");
38  return -1;
39 }

◆ modifiedBinarySearch()

void modifiedBinarySearch ( const int **  mat,
int  n,
int  m,
int  x 
)

Function to perform binary search on the mid values of row to get the desired pair of rows where the element can be found.

Parameters
[in]matmatrix to search for the value in
nnumber of rows in the matrix
mnumber of columns in the matrix
xvalue to search for
49 { // If Single row matrix
50  if (n == 1)
51  {
52  binarySearch(mat, 0, 0, m - 1, x);
53  return;
54  }
55 
56  // Do binary search in middle column.
57  // Condition to terminate the loop when the 2 desired rows are found.
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  // element found
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  // If element is present on the mid of the two rows
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  // Search element on 1st half of 1st row
80  else if (x <= mat[i_low][j_mid - 1])
81  binarySearch(mat, i_low, 0, j_mid - 1, x);
82 
83  // Search element on 2nd half of 1st row
84  else if (x >= mat[i_low][j_mid + 1] && x <= mat[i_low][m - 1])
85  binarySearch(mat, i_low, j_mid + 1, m - 1, x);
86 
87  // Search element on 1st half of 2nd row
88  else if (x <= mat[i_low + 1][j_mid - 1])
89  binarySearch(mat, i_low + 1, 0, j_mid - 1, x);
90 
91  // search element on 2nd half of 2nd row
92  else
93  binarySearch(mat, i_low + 1, j_mid + 1, m - 1, x);
94 }
Here is the call graph for this function:
binarySearch
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