Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
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. More...
 

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}

◆ main()

int main ( void  )

Main function.

98{
99 int x; // element to be searched
100 int m, n; // m = columns, n = rows
101
102 scanf("%d %d %d\n", &n, &m, &x);
103
104 int **mat = (int **)malloc(n * sizeof(int *));
105 for (int i = 0; i < m; i++) mat[i] = (int *)malloc(m * sizeof(int));
106
107 for (int i = 0; i < n; i++)
108 {
109 for (int j = 0; j < m; j++)
110 {
111 scanf("%d", &mat[i][j]);
112 }
113 }
114
115 modifiedBinarySearch(mat, n, m, x);
116
117 for (int i = 0; i < n; i++) free(mat[i]);
118 free(mat);
119 return 0;
120}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26
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 ...
Definition: modified_binary_search.c:48
Here is the call graph for this function:

◆ 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}
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
Here is the call graph for this function: