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.
- Start with the bottom left element
- Loop: compare this element e with x
- if they are equal then return its position
- e x then move it to right (if out of bound of the matrix then break return false)
- repeat the 1, 2 and 3 till you find an element or returned false
Thanks to devendraiiit for suggesting below approach.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
#define MAX 100
bool search( int mat[][MAX], int m, int n, int x)
{
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
j++;
}
return false ;
}
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
class GFG
{
static final int MAX = 100 ;
static boolean search( int mat[][], int m,
int n, int x)
{
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
j++;
}
return false ;
}
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" );
}
}
|
Python3
def search(mat, m, n, x):
i, j = m - 1 , 0
while (i > = 0 and j < n):
if (mat[i][j] = = x):
return True ;
if (mat[i][j] > x):
i - = 1
else :
j + = 1
return False
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" )
|
C#
using System;
class GFG
{
static bool search( int [,] mat, int m,
int n, int x)
{
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
j++;
}
return false ;
}
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" );
}
}
|
PHP
<?php
$MAX = 100;
function search( $mat , $m , $n , $x )
{
$i = $m - 1;
$j = 0;
while ( $i >= 0 && $j < $n )
{
if ( $mat [ $i ][ $j ] == $x )
return true;
if ( $mat [ $i ][ $j ] > $x )
$i --;
else
$j ++;
}
return false;
}
$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" ;
?>
|
Javascript
<script>
function search(mat, m, n, x)
{
var i = m - 1, j = 0;
while (i >= 0 && j < n)
{
if (mat[i][j] == x)
return true ;
if (mat[i][j] > x)
i--;
else
j++;
}
return false ;
}
var 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))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(m + n)
Auxiliary Space: O(1)
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.
Last Updated :
07 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...