Saddleback Search Algorithm in a 2D array
Find an element in a given matrix such that each row and each column is sorted.
Examples:
Input : arr[] = {
{ 1, 2, 3},
{ 4, 5, 6},
{ 7, 8, 9}
}
element=5
Output : Element Found at position (1, 1).
Input : arr[]={
{ 11, 21, 31, 41, 51 },
{ 12, 22, 32, 42, 52 },
{ 13, 23, 33, 43, 53 },
{ 14, 24, 34, 44, 54 },
{ 15, 25, 35, 45, 55 }
}
element=11
Output : Element Found at position (0, 0).
A simple solution is to search one by one. Time complexity of this solution is O(n2).
A better solution is to use Divide and Conquer to find the element. Time complexity of this solution is O(n1.58). Please refer this article for details.
Below is an efficient solution that works in O(m + n) time.
1) Start with the bottom left element
2) Loop: compare this element e with x
….i) if they are equal then return its position
…ii) e x then move it to right (if out of bound of the matrix then break return false)
3) repeat the i), ii) and iii) till you find an element or returned false
Thanks to devendraiiit for suggesting below approach.
Implementation:
C++
// C++ program to search an element in row-wise // and column-wise sorted matrix #include<bits/stdc++.h> using namespace std; #define MAX 100 /* Searches the element x in mat[m][n]. If the element is found, then prints its position and returns true, otherwise prints "not found" and returns false */bool search(int mat[][MAX], int m, int n, int x) { int i = m-1, j = 0; //set indexes for bottom left element while ( i >= 0 && j < n ) { if ( mat[i][j] == x ) return true; if ( mat[i][j] > x ) i--; else // if mat[i][j] < x j++; } return false; } // driver program to test above function int main() { int mat[][MAX] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}, {50, 60, 70, 80}, }; if (search(mat, 5, 4, 29)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java program to search an // element in row-wise and // column-wise sorted matrix class GFG { static final int MAX = 100; /* Searches the element x in mat[m][n]. If the element is found, then prints its position and returns true, otherwise prints "not found" and returns false */static boolean search(int mat[][], int m, int n, int x) { // set indexes for // bottom left element int i = m - 1, j = 0; while (i >= 0 && j < n) { if (mat[i][j] == x) return true; if (mat[i][j] > x) i--; else // if mat[i][j] < x j++; } return false; } // Driver Code public static void main(String args[]) { int mat[][] = {{10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}, {50, 60, 70, 80}}; if (search(mat, 5, 4, 29)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed // by Kirti_Mangal |
Python3
# Python program to search an element in # row-wise and column-wise sorted matrix # define MAX 100 # Searches the element x in mat[m][n]. # If the element is found, then prints # its position and returns true, otherwise # prints "not found" and returns false def search(mat, m, n, x): i, j = m - 1, 0 # set indexes for bottom # left element while (i >= 0 and j < n): if (mat[i][j] == x): return True; if (mat[i][j] > x): i -= 1 else: # if mat[i][j] < x j += 1 return False # Driver Code if __name__ == '__main__': mat = [[10, 20, 30, 40], [15, 25, 35, 45], [27, 29, 37, 48], [32, 33, 39, 50], [50, 60, 70, 80]] if (search(mat, 5, 4, 29)): print("Yes") else: print("No") # This code is contributed by Rajput-Ji |
C#
// C# program to search an // element in row-wise and // column-wise sorted matrix using System; class GFG { /* Searches the element x in mat[m][n]. If the element is found, then prints its position and returns true, otherwise prints "not found" and returns false */static bool search(int[,] mat, int m, int n, int x) { // set indexes for // bottom left element int i = m - 1, j = 0; while (i >= 0 && j < n) { if (mat[i, j] == x) return true; if (mat[i, j] > x) i--; else // if mat[i][j] < x j++; } return false; } // Driver Code public static void Main() { int [,]mat = {{10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}, {50, 60, 70, 80}}; if (search(mat, 5, 4, 29)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
PHP
= 0 && $j < $n)
{
if ($mat[$i][$j] == $x)
return true;
if ($mat[$i][$j] > $x)
$i–;
else // if mat[i][j] < x
$j++;
}
return false;
}
// Driver Code
$mat = array(array(10, 20, 30, 40),
array(15, 25, 35, 45),
array(27, 29, 37, 48),
array(32, 33, 39, 50),
array(50, 60, 70, 80));
if (search($mat, 5, 4, 29))
echo "Yes";
else
echo "No";
// This code is contributed by mits
?>
Yes
Time Complexity: O(m + n)
The above can also be implemented by starting from the top right corner. Please see search in a row-wise and column wise sorted matrix for the alternate implementation.
Recommended Posts:
- Randomized Binary Search Algorithm
- Search in an almost sorted array
- Efficient search in an array where difference between adjacent is 1
- Search an element in a sorted and rotated array
- Front and Back Search in unsorted array
- Create a Sorted Array Using Binary Search
- Search, insert and delete in an unsorted array
- Search, insert and delete in a sorted array
- Recursive program to linearly search an element in a given array
- Find final value if we double after every successful search in array
- Search an element in an array where difference between adjacent elements is 1
- Search an element in an unsorted array using minimum number of comparisons
- Search equal, bigger or smaller in a sorted array in Java
- Repeatedly search an element by doubling it after every successful search
- Why is Binary Search preferred over Ternary Search?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



